Skip first line of fgetcsv method

I have a CSV file and I read data from CSV file then I want to skip first line of CSV file.Which'll contain any header. I am using this code.

while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
    // Code to insert into database
}

When I insert data into th database then header should not to be saved into the database.


Before beginning the while loop, just get the first line and do nothing with it. This way the logic to test if it's the first line is not needed.

fgetcsv($file, 10000, ",");
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) {
  //....
}

try:

$flag = true;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) {
   if($flag) { $flag = false; continue; }
   // rest of your code
}

A bit late, but here is another way to do so (without having to count all the lines): with fgets

$file = fopen($filename, 'r');  // create handler
fgets($file);  // read one line for nothing (skip header)
while (($line = fgetcsv($file, 10000, ",")) !== FALSE) {
    // do your thing
}

One might consider this to be more elegant


You can add a simple check and skip the query if the check fails:

$firstline = true;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
    if (!$firstline) {
        // Code to insert into database
    }
    $firstline = false;
}