1. Home
  2. Blog
  3. Contact

Quickly reading CSV files with PHP

Posted on November 29th 2009 at 21:28 inside PHP with 2 comments

So why would we want to read data from a CSV file? Well I will give an example, not so long ago a customer requested that data from his back-office system would be imported into his website on a periodic base. The data from this system was supplied was in the CSV format, and of course contained a lot of bogus and unnecesary data. (Otherwise we could have just done it with simple SQL).

Some examples

The first step in this process would be to read the contents of the file. So if we would be unaware of the fact that there are some built-in PHP functions we can use, we would probably use something like this:

Read the entire file, bit by bit, into a string 

  1. $r_handle = fopen('somefile.csv','r');
  2. $s_contents = '';
  3. while(!feof($r_handle)) {
  4.     $s_contents .= fread($r_handle,8192);
  5. }
  6. fclose($r_handle);
$r_handle = fopen('somefile.csv','r');
$s_contents = '';
while(!feof($r_handle)) {
    $s_contents .= fread($r_handle,8192);


Read the entire file into a string 

  1. $s_contents = file_get_contents('somefile.csv');
$s_contents = file_get_contents('somefile.csv');

or even

Read the entire file, putting every line as an array entry 

  1. $a_lines = file('somefile.csv');
$a_lines = file('somefile.csv');

Explaining the problem

This gives us some disadvantages, first of all we need to read the entire file before we can use it. This will give us problems when reading files that are larger that the PHP memory limit.

The second problem is that we need to process every line/row in the CSV. We need to filter out quotes where they are not needed. This can be a real pain to do so.

The solution!

Sounds like an "as-seen-on-tv" commercial doesn't it? Anyway, the solution is not so complicated. PHP has 2 nice function to handle CSV reading, and writing!

The functions 


So our first method of reading a file was not so far off. Knowing about these functions now we can use them as following:

Reading each row from the CSV file into an array 

  1. $r_handle = fopen('somefile.csv','r');
  2. while ($a_csvData = fgetcsv($r_handle, 10240, ',', '"') !== false) {
  3.     // Do whatever you would like to do with each row in the CSV
  4. }
  5. fclose($r_handle);
$r_handle = fopen('somefile.csv','r');
while ($a_csvData = fgetcsv($r_handle, 10240, ',', '"') !== false) {
    // Do whatever you would like to do with each row in the CSV

Using this, we can now proccess each CSV row as an array. This makes it a lot easier to process the data of any row and column, and also takes away the problem of having to read the entire file before being able to do so.


php csv fgetcsv fputcsv quick read write

Comments (2)

Gravatar: Nitin SewtahalsingNitin Sewtahalsing2009-12-16 15:41


This is what i was looking for.

Nice and simple!

Gravatar: ColinColin2015-04-16 15:12

Bugging you with your bug -

$a_csvData = fgetcsv($r_handle, 10240, ',', '"') !== false
evaluates to true, so
$a_csvData doesn't hold the data
you need brackets
while ( ($a_csvData = fgetcsv($r_handle, 10240, ',', '"') )!== false

Your comment