Php tutorial fputcsv () function csv data read/write database tutorial File Code
The fputcsv () function is used to write data into a file or database in csv format.
1. Write the string to the csv file.
$ Test_array = array (
Array ("111", "sdfsd", "sdds", "43344", "rrrr "),
Array ("sssssssss", "gdfgd", "232323", "wwewe", "dsfds "),
Array ("fgg", "e4343", "dsfds", "w2332", "xcvxc "),
Array ("11212", "2323", "344343", "344343", "rerreer "),
Array ("fds", "43344444", "33333333", "ttttttt", "gggggggggg "),
Array ("kdfs", "dsfdsfds", "wewewe", "sdsdddddddddd", "wwwwwwwwwww ")
);
$ 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. "<br> ";
If ($ isSuccess === false)
{
Die ("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 row or false. If the write fails, false is returned.
2. Save the formatted csv string to the string.
$ Test_array = array (
Array ("111", "sdfsd", "sdds", "43344", "rrrr "),
Array ("sssssssss", "gdfgd", "232323", "wwewe", "dsfds "),
Array ("fgg", "e4343", "dsfds", "w2332", "xcvxc "),
Array ("11212", "2323", "344343", "344343", "rerreer "),
Array ("fds", "43344444", "33333333", "ttttttt", "gggggggggg "),
Array ("kdfs", "dsfdsfds", "wewewe", "sdsdddddddddd", "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 ($ isSuccess === false)
{
Die ("Can't write csv line". $ line_array );
}
}
Fclose ($ file) or die ("Can't close file test.csv .");
$ Result = ob_get_contents ();
Ob_end_clean ();
Use the fgetcsv (file, length, separator, enclosure) function to read the csv file.
Fgetcsv parameters are described as follows:
File: csv file to be read. this parameter is required.
Length: The value greater than the length of the longest row in the csv file. Php5 is a required parameter. It is an optional parameter in php5. If you do not set this parameter or set it to 0, php will read
A whole row of data. If the length of a row exceeds 8192 bytes, set the length value to a number instead of letting php automatically calculate the length of the row.
Separator: Specifies the Data separator. The default Delimiter is a comma. If it is specified as ";", the fgetcsv function parses row data according.
Return Value of fgetcsv:
Returns an array based on a row of data in the file. If an error occurs while reading the file, false is returned. When the end of the file is reached, false is also returned.
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 '<table border = 0> ';
While ($ csv_line = fgetcsv ($ file ))
{
Print "<tr> ";
$ Len = count ($ csv_line );
For ($ I = 0; $ I <$ len; $ I ++)
{
If ($ I % 2 = 0) $ color = "# cccccc ";
Else $ color = "#999999 ";
Print '<td bgcolor ='. $ color. '> '.html entities ($ csv_line [$ I]).' </td> ';
}
Print "</tr> ";
}
Print '</table> ';
Fclose ($ file) or die ("Can't close file test.csv! ");