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  

Comments

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

/Users/Heike/Desktop/250K-f10-080608.csv.download

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 -->> http://www.upgradedtutorials.info/tutorials/how-to-upload-a-csv-file-and-save-to-database-using-php/

You need to login to post a comment.