Home > Backend Development > PHP Tutorial > php fputcsv() function csv data reading and writing database file code_PHP tutorial

php fputcsv() function csv data reading and writing database file code_PHP tutorial

WBOY
Release: 2016-07-20 11:09:48
Original
1104 people have browsed it

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

$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.

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","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.
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


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 '';
        }
        print "";
    }
    print '
'.htmlentities($csv_line[$i]).'
';
    fclose($file) or die("Can't close file test.csv!");


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444771.htmlTechArticlephp教程 fputcsv() 函数csv数据读写数据库教程文件代码 fputcsv() 函数用于将数据格式为csv格式,以便写入文件或者数据库。 1.将字符串写入cs...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template