1
2
3
4
5
6
7
8
9
|
<?
$filename = 'blog.csv';
$fp = fopen($filename, 'w');
$output = " Hello ";
$output .= " World! ";
$output .= "\r\n";
fputs($fp, $output);
fclose($fp);
?>
|
1
2
3
4
5
6
7
8
9
|
<?
$filename = 'blog.csv';
$fp = fopen($filename, 'w');
$output = " Hello ";
$output .= " World! ";
$output .= "\r\n";
fputs($fp, $output);
fclose($fp);
?>
|
"id","dealer_id","vin","stockno",
"1","2","3","4",
"5","6","7","8",
"9","10","11",12"
"id"=>"1", "dealer_id"=>"2", "vin"=>"3", "stockno"=>"4"
"id"=>"1", "dealer_id"=>"2", "vin"=>"3", "stockno"=>"4"
"id"=>"5", "dealer_id"=>"6", "vin"=>"7", "stockno"=>"8"
"id"=>"9", "dealer_id"=>"10", "vin"=>"11", "stockno"=>"12"
<?php
$file = fopen('data.txt', 'r');
$headers = fgetcsv($file);
$result = array();
while ($row = fgetcsv($file)) {
if (!$row[0]) continue;
$nextItem = array();
for ($i = 0; $i < 4; ++$i) {
$nextItem[$headers[$i]] = $row[$i];
}
$result[] = $nextItem;
}
fclose($file);
var_dump($result);
fgetcsv to read each delimited row of the file. The first row is used as the header names. Then, for each other row, we create a new entry and match the indexes to the header names. I set it to 4 because you have a trailing delimiter that creates an empty entry at the end, if you removed those from the input you could use the length of $headers for the inner loop.var_dump for your data:array(3) {
[0]=>
array(4) {
["id"]=>
string(1) "1"
["dealer_id"]=>
string(1) "2"
["vin"]=>
string(1) "3"
["stockno"]=>
string(1) "4"
}
[1]=>
array(4) {
["id"]=>
string(1) "5"
["dealer_id"]=>
string(1) "6"
["vin"]=>
string(1) "7"
["stockno"]=>
string(1) "8"
}
[2]=>
array(4) {
["id"]=>
string(1) "9"
["dealer_id"]=>
string(2) "10"
["vin"]=>
string(2) "11"
["stockno"]=>
string(3) "12""
}
}
<title>Image to Base64 String</title>
<fieldset>
<legend>Image to Base64 String</legend>
<center>
<form name="select_all">
<?php
/**
* This code will help you to learn how we can convert an image into a base64 string!!
*/
echo"<h3><p>Image</p></h3>";
//$file = File Image yang ingin di encode
//Filetype: JPEG,PNG,GIF
$file = "encode.jpg";
if($fp = fopen($file,"rb", 0))
{
$gambar = fread($fp,filesize($file));
fclose($fp);
$base64 = chunk_split(base64_encode($gambar));
//Result
$encode = '<img src="data:image/jpg/png/gif;base64,' . $base64 .'" >';
echo $encode;
}
?>
<br><textarea name="text_area" rows="20" cols="70"> <? echo $encode; ?> </textarea>
<p><input type="button" value="Select All Code" onClick="javascript:this.form.text_area.focus();this.form.text_area.select();"></p>
</form>
</center>
</fieldset>
| Mode | Purpose |
|---|---|
| r | Opens the file for reading only. Places the file pointer at the beginning of the file. |
| r+ | Opens the file for reading and writing. Places the file pointer at the beginning of the file. |
| w | Opens the file for writing only. Places the file pointer at the beginning of the file. and truncates the file to zero length. If files does not exist then it attemts to create a file. |
| w+ | Opens the file for reading and writing only. Places the file pointer at the beginning of the file. and truncates the file to zero length. If files does not exist then it attemts to create a file. |
| a | Opens the file for writing only. Places the file pointer at the end of the file. If files does not exist then it attemts to create a file. |
| a+ | Opens the file for reading and writing only. Places the file pointer at the end of the file. If files does not exist then it attemts to create a file. |
<html>
<head>
<title>Reading a file using PHP</title>
</head>
<body>
<?php
$filename = "/home/user/guest/tmp.txt";
$file = fopen( $filename, "r" );
if( $file == false )
{
echo ( "Error in opening file" );
exit();
}
$filesize = filesize( $filename );
$filetext = fread( $file, $filesize );
fclose( $file );
echo ( "File size : $filesize bytes" );
echo ( "<pre>$filetext</pre>" );
?>
</body>
</html>
|
<?php
$filename = "/home/user/guest/newfile.txt";
$file = fopen( $filename, "w" );
if( $file == false )
{
echo ( "Error in opening new file" );
exit();
}
fwrite( $file, "This is a simple test\n" );
fclose( $file );
?>
<html>
<head>
<title>Writing a file using PHP</title>
</head>
<body>
<?php
if( file_exist( $filename ) )
{
$filesize = filesize( $filename );
$msg = "File created with name $filename ";
$msg .= "containing $filesize bytes";
echo ($msg );
}
else
{
echo ("File $filename does not exit" );
}
?>
</body>
</html>
|
Hello Friends! I am Ramana a part time blogger from Hyderabad.