开发者

How to read large files line by line? [duplicate]

This question already has answers here: Closed 11 years ago.

Possibl开发者_开发问答e Duplicate:

how to extract data from csv file in php

I need to do it for a huge CSV file with help of PHP.


Use fgetcsv function. It gets a line from file pointer and parse for CSV fields.

Following example reads a CSV file myfile.csv, gets the records and displays them line by line.

<?php
$row = 1;
//open the file
if (($handle = fopen("myfile.csv", "r")) !== FALSE)
{
    while (($data = fgetcsv($handle, 0, ",")) !== FALSE)
    {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++)
        {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜