Php fputcsv () function csv data read/write database file code

Source: Internet
Author: User
Tags foreach php tutorial

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! ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.