Simple CSV loading in PHP


Ever had the need to load a Comma Separated Values (CSV) file in PHP? This simple code block will do it quickly.

// True if the file we wish to load has a header row
$first = true;

// The file resource we wish to process - only needs read rights
$file = fopen('myfile.csv', 'r');

// Iterate over the rows 
while (($row = fgetcsv($file)) !== false) {

   // Skip to next iteration if first (header) row
   if ($first) {
      $first = false;

   // Your logic goes here e.g.
   echo $row[0].' '.$row[1].'\n';