Fgetcsv skip first line

I have searched the archives and could not find what I need.

I have this code, which works great for uploading the entire csv file into my database :

[COLOR="green"]$objCSV = fopen("data/".$_FILES["fileCSV"]["name"], "r");

while (($objArr = fgetcsv($objCSV, 1000, ",")) !== FALSE) {
	$strSQL = "INSERT INTO cvs ( id, data_a, data_b, data_c, data_d )  VALUES ( '$objArr[0]', '$objArr[1]', '$objArr[2]', '$objArr[3]', '$objArr[4]') ";
	$objQuery = mysql_query($strSQL);
}[/COLOR]

I need to skip line (1) so as NOT to upload the field headers.
Any help would be appreciated

$flag = true;
while (($objArr = fgetcsv($objCSV, 1000, “,”)) !== FALSE) {
if($flag) { $flag = false; continue; }
$strSQL = "INSERT INTO cvs ( id, data_a, data_b, data_c, data_d ) VALUES ( ‘$objArr[0]’, ‘$objArr[1]’, ‘$objArr[2]’, ‘$objArr[3]’, ‘$objArr[4]’) ";
$objQuery = mysql_query($strSQL);
}

Welcome to Sitepoint CowboyTed.

You could also look to use SplFileObject, as it’s iterable, you can use the LimitIterator on it to skip the first line.


<?php
$csv = new SplFileObject('path/to/file.csv', 'r');
$csv->setFlags(SplFileObject::READ_CSV);

foreach(new LimitIterator($csv, 1) as $line){
  var_dump($line); #it's a numerically indexed array
}

This might be of interest too: https://gist.github.com/485810

Thanks StarLion,

How exactly does that code cause the first line to be skipped. (So I have this clear in my mind for future use)

Thanks Anthony,

Reading some of your posts from this site (from google) is what convinced me that this may be a site worth joining.

I am not familiar with SplFileObject - could you expalin a little more,… I just saw you posted a link as well and I will read it too.

Gee, thanks. :slight_smile:

I’ve added some comments to help, hopefully.


<?php
/*
  Open the CSV file for reading (r)
*/
$csv = new SplFileObject('path/to/file.csv', 'r');

/*
  Tell the object that this file should
  be treated as a CSV
*/
$csv->setFlags(SplFileObject::READ_CSV);

/*
  Wrap the CSV file in a filter which
  skips the first line
*/
$csv = new LimitIterator($csv, 1);

/*
  Loops over the CSV file, the SplFileObject
  automatically creates an array for you
*/
foreach($csv as $line){
  var_dump($line); #it's a numerically indexed array
}

The flag is set to true outside the loop.
The first line gets read.
The IF statement finds that the flag is true; sets the flag to false, and goes back to the beginning of the loop (Continue)
The second line gets read. Now the flag is false, so the if doesnt execute, and the code procedes as normal.

Anthony, nice - thanks for mentioning that method.

Can you name any another benefits of using the SPL library to do that?

Thanks,

I used StarLion’s modification although I am not quite sure how it works. It works though!! But if I don’t understand why it works then I have only hacked it.

I will try your idea out as well,… I will have to look at it a bit and see how to work it into my code.

As I am writing this I see StarLion’s reply,… oh my I cannot keep up with you guys :slight_smile:

Thanks so much,
I really think I found a good forum at last!!!

Thanks StarLion,

That is what I was thinking but I wasn’t sure. It sure was an “easy” way to fix this issue. I do want to get into Anthoy’s idea too.

I am by no means a guru but I am well along in this crazy language and am hooked!
I keep getting deeper and deeper and have found there is no end to learning,…

Thanks again