Home >Backend Development >PHP Tutorial >php fputcsv() function csv data reading and writing database file code_PHP tutorial
php tutorial fputcsv() function csv data reading and writing database tutorial file code
fputcsv() function is used to format data into csv format for writing to a file or database.
1. Write the string into the csv file
2. Save the formatted csv string into a string.$test_array = array(
}
array("111","sdfsd"," sdds","43344","rrrr"),
array("sssssssss","gdfgfd","232323","wwewe","dsfds"),
array("fgfg","e4343" ,"dsfds","w2332","xcvxc"),
array("11212","2323","344343","344343","rerreer"),
array("fds"," 43344444","33333333","ttttttt","gggggggggggg"),
array("kdfs","dsfdsfds","wewewe","sdsdddddddd","wwwwwwwww")
);
$file = fopen("test.csv","w") or die("Can't Open test.csv");
foreach($test_array as $line_array)
{
$ isSuccess = fputcsv($file,$line_array);
print $isSuccess."
"; Can't write csv line".$line_array);}
fclose($file) or die("Can't close file test.csv.");
The fputcsv() function returns the number of characters in the written line or false. It returns false when the writing fails.
$test_array = array(
array("111","sdfsd","sdds","43344","rrrr"),array("sssssssss ","gdfgfd","232323","wwewe","dsfds"),
array("fgfg","e4343","dsfds","w2332","xcvxc"),array( "11212","2323","344343","344343","rerreer"),length: Indicates a value greater than the length of the longest line in the csv file. It is a required parameter before php5. It is an optional parameter in php5. If this parameter is not set or set to 0, php will read
array("fds","43344444","33333333","ttttttt","gggggggggggg"),
array("kdfs","dsfdsfds","wewewe","sdsdddddddd","wwwwwwwwwww")
);
ob_start();
$file = fopen("php://output", "w") or die("Can't Open php://output");
foreach($test_array as $line_array)
{
$isSuccess = fputcsv($file,$line_array);
if ($ issSuccess === false)
{
DIE ("can'T write csv line". $ LINE_ARAY); $file) or die("Can't close file test.csv.");
$result = ob_get_contents();
ob_end_clean();
to use fgetcsv(file ,length,separator,enclosure) function reads the csv file.
The parameter description of fgetcsv is as follows:
file: The csv file that needs to be read, this parameter is required.
a whole row of data. If the length of the line exceeds 8192 bytes, the length value should be set to a number instead of letting PHP automatically calculate the length of the line.
separator: Specify the separator of data, the default is comma, if specified as ";", then the fgetcsv function will parse the row data according to ";".
The return value of fgetcsv:
Returns an array based on a row of data in file. If an error occurred while reading the file, false is returned. It also returns false when the end of the file is reached.
The following is an example of reading the test.csv file.
$file = fopen('test.csv','r') or die("Can't open file test.csv");
$color="#ff0000";
print '';
';
while($csv_line=fgetcsv($file))
{
print ""; ";
$len = count($csv_line);
for($i=0;$i<$len;$i++)
{
if($i%2==0)$color="#cccccc";
else $color="#999999";
print ''.htmlentities($csv_line[$i]).' ';
}
print "
}
print '
fclose($file) or die("Can't close file test.csv!");