SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot dizyn's Avatar
    Join Date
    Apr 2006
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reading csv file by line

    Hi all


    I want to read a csv file line by line. What should be the 2nd argument of fread so that i can read line by line

    PHP Code:

    if (!$handle fopen($filename'w+'))
    {
        echo 
    "Cannot open file ($filename)";
        exit;
    }
    else
    {
            
    fread($handle,....);
            
    fclose($handle);


  2. #2
    SitePoint Zealot dizyn's Avatar
    Join Date
    Apr 2006
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this worked.
    while (!feof($handle))
    {
    $line = fgets($handle);
    echo $line.'<BR/>';
    }

  3. #3
    SitePoint Addict wibble wobble's Avatar
    Join Date
    Dec 2008
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use fgetcsv() instead.

    Example from http://uk3.php.net/fgetcsv :
    PHP Code:
    <?php
    $row 
    1;
    $handle fopen("test.csv""r");
    while ((
    $data fgetcsv($handle1000",")) !== 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);
    ?>
    Find freelance jobs from all the major sites in one place:
    on twitter / on the web / twitter rss feed


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •