Import CSV File

 / Published in: PHP

This code reads a CSV file and extracts each item into the appropriate variable for processing.

  1. $handle = fopen("file.csv", 'r');
  2. while(($data = fgetcsv($handle, 1000, ",")) !== false)
  3. {
  4. list($col1, $col2, ...) = $data;
  5. }
  6. fclose($handle);

Report this snippet  


RSS Icon Subscribe to comments
Posted By: paula on October 25, 2008


Posted By: shaunduncan on July 13, 2010

Just as a pointer, calling fgetcsv() using the length of 1000 MUST ensure that the longest line is at MOST 1000 characters long. Also, for generally large CSV files, calling list() to load column into a variable may not be advantageous...the $data variable returned is an indexed array of each column.

Posted By: upgradedtutorials on January 27, 2013

Here's also a good tutorial on How To Upload a CSV file and Save To Database using PHP -->>

You need to login to post a comment.