Jump to content

read a csv file line-by-line


lordrt

Recommended Posts

I have a csv file which contains values as follows (the line numbers are not found in the file):

 

line1: 1, 2, 3, 4, 5, 6, 7, 8

line2: 9, 10, 11, 12, .....

....

lineN: ...................

 

I have to read the file one line at a time, and for each line read there will be a process which will be executed. Am trying the fgetcsv function but not really understanding the concept behind it.

Can anyone help with some codes on how to call, open and read the csv file??

Link to comment
https://forums.phpfreaks.com/topic/168772-read-a-csv-file-line-by-line/
Share on other sites

I'm not sure we could write better example than the one in the manual....

 

<?php
$row = 1;
$handle = fopen("test.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== 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);
?>

 

What part don't you get?

Does the above code read line-by-line, coz i have to read a line, do some ops on it, then read a new line and so on

 

 

<?php
$handle = fopen("file.txt","r");
if($handle){
while(!feof($handle)){
    

     $line = fgets($handle,1024); 
     
   // line1: 1, 2, 3, 4, 5, 6, 7, 8

  }
fclose($handle);
}

?>

sorry, code goes like this:

 

<?php

 

//import php file: import_article.php

require("import_article.php");

 

//

mysql_connect("127.0.0.1", "root", "") or die (mysql_error());

mysql_select_db("drupal_db") or die (mysql_error());

 

//read first .csv file encountered

 

$path = "sites/path/files/";

$sfile = "*.csv";

 

foreach (glob($path.$sfile) as $filename){

echo $filename . "<br/>";

$file = fopen($filename, 'r');

    while (($data = fgetcsv($file, 1000, ",")) !== False)

    {

        $result = ImportArticle($data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6]);

        unset ($result);

    }

echo "read complete";

    fclose($file);

//unlink ($filename);

}

//echo "closing connection";

mysql_close();

?>

 

The 'ImportArticle' is a function found in the php file being called by require() which contains php mysql statements to update/insert data into tables

 

An example of my csv is (exclude line1,2) and text are not wrapped in editor:

line1: 103, 'Ball. Rustique', '<p><img width="500" height="300" src="/.../103.jpg" alt="" /></p><p> </p>Ball. Rustique', 0.8, '103.jpg', '/.../103.jpg', 0

 

line2: 60, 'Petit pain allongé m', '<p>Petit pain allongé m</p>', 0.6, ' ', ' ', 0

They already gave you the perfect thing

 

<?php
$handle = fopen("file.txt","r");
if($handle){
while(!feof($handle)){
    

     $line = fgets($handle,1024); 
     
   // line1: 1, 2, 3, 4, 5, 6, 7, 8

  }
fclose($handle);
}

?>

 

Now I suggest you actually try it for yourself instead of asking other people to do your testing for you. You should really comeback if you have problems, not because you don't feel like testing it

in the example i gave above, for each line it ends with something like a "\n" if converted to html, so do I have to include this in the php script as well so as to know where a line ends
No!

fgetcsv() reads only a single line from the file each time it is called.

thanks for ur help guys, for reading a text file with fgets and doing the ops on a db works fine, it was only the csv which confused me somehow. In fact I was using the same code below to read my data from .txt files, but now was asked to use csv to do the reading of data

 

[pre]<?php

 

//connecting to host db

 

mysql_connect("127.0.0.1", "root", "") or die (mysql_error());

echo "Connected to host" . "<br/>";

 

mysql_select_db("drupal_db") or die(mysql_error());

echo "Connected to DB" . "<br/>";

 

//read first .ark file encountered

 

$path = "sites/zenhaeusern-sion.sw/files/Arkeio/";

$sfile = "*.txt";

 

foreach (glob($path.$sfile) as $filename){

echo $filename . "<br/>";

$file = fopen($filename, 'r');

    while (!feof($file)){

        $data = fgets($file);

      echo $data . "<br/>";

mysql_query($data);

        unset ($data);

    }

echo "read complete";

    fclose($file);

unlink ($filename);

break;

}

echo "closing connection";

mysql_close();

?>

[/pre]

 

sorry, code goes like this:

 

<?php

 

//import php file: import_article.php

require("import_article.php");

 

//

mysql_connect("127.0.0.1", "root", "") or die (mysql_error());

mysql_select_db("drupal_db") or die (mysql_error());

 

//read first .csv file encountered

 

$path = "sites/path/files/";

$sfile = "*.csv";

 

foreach (glob($path.$sfile) as $filename){

echo $filename . "<br/>";

$file = fopen($filename, 'r');

    while (($data = fgetcsv($file, 1000, ",")) !== False)

    {

        $result = ImportArticle($data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6]);

        unset ($result);

    }

echo "read complete";

    fclose($file);

//unlink ($filename);

}

//echo "closing connection";

mysql_close();

?>

 

The 'ImportArticle' is a function found in the php file being called by require() which contains php mysql statements to update/insert data into tables

 

An example of my csv is (exclude line1,2) and text are not wrapped in editor:

line1: 103, 'Ball. Rustique', '<p><img width="500" height="300" src="/.../103.jpg" alt="" /></p><p> </p>Ball. Rustique', 0.8, '103.jpg', '/.../103.jpg', 0

 

line2: 60, 'Petit pain allongé m', '<p>Petit pain allongé m</p>', 0.6, ' ', ' ', 0

 

The code above is running only for the 1st line, it does not erad 2nd line, any help plz?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.