Your Ad Here

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.

You need to login to post a comment.