Kieran Headley
Kieran Headley

Reputation: 993

How to skip the first line of CSV in PHP

I have a CSV upload that I am struggling to get to skip the first line of the CSV document. I am uploading a single CSV document and the first line contains a cell that contains one bit of text which is throwing out the array. I am not sure which count to edit?

$fields_firstrow = true;
$i = 0; 
$a = 0;
$fields = array();
$content = array(); 

$allowedExts = array("csv");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["size"] < 2000000)&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {

    if (file_exists($_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],$_FILES["file"]["name"]);
      }
    }
  }
else
  {
  echo "Invalid file";
  }
$file = $_FILES["file"]["name"];

if (($handle = fopen($file, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 0, ",")) !== FALSE) { 
        if($fields_firstrow == true && $i<1) {
            foreach($data as $d) {
                $fields[] = strtolower(str_replace(" ", "_", $d));
            }
            $i++; 
            continue;
        }
        $c = 0;
        foreach($data as $d) {
            if($fields_firstrow == true) {
                $content[$a][$fields[$c]] = $d;
            } else {
                $content[$a][$c] = $d;
            }
            $c++;
        }
        $a++;
    }
} else { 
    echo "Could not open file"; 
    die();
}

Any help would be greatly appreciated.

Upvotes: 0

Views: 2859

Answers (4)

Rahul Gupta
Rahul Gupta

Reputation: 10161

Just add an extra line of code before the line from where the while loop starts as shown below :

....
.....
fgetcsv($handle);//Adding this line will skip the reading of th first line from the csv file and the reading process will begin from the second line onwards
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
.......
.......

It is just as simple........ !!!

Upvotes: 1

lobner
lobner

Reputation: 593

Here is an example from http://php.net/fgets modified a bit:

<?php
$handle = @fopen("/tmp/inputfile.txt", "r");
$firstLine = true;
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
    if(firstLine) {
        $firstLine = false;
        continue;
    }
    echo $buffer;
}
if (!feof($handle)) {
    echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
?>

I assume you see the point, and can modify your script accordingly.

Upvotes: 0

Jose Areas
Jose Areas

Reputation: 719

You are not changing the value for variable $fields_firstrow. For all loop iteration it will still be true.

In my opinion and per my understand of your code, you should change it to false before the first continue.

...
if (($handle = fopen($file, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 0, ",")) !== FALSE) { 
        if($fields_firstrow == true && $i<1) {
            foreach($data as $d) {
                $fields[] = strtolower(str_replace(" ", "_", $d));
            }
            $i++; 
            $fields_firstrow = false;
            continue;
        }
        $c = 0;
        foreach($data as $d) {
            if($fields_firstrow == true) {
                $content[$a][$fields[$c]] = $d;
            } else {
...

Maybe you do not need the $i variable after that.

Upvotes: 0

Ali Mohammadi
Ali Mohammadi

Reputation: 1324

    $i=0;
    if($fields_firstrow == true) {
        foreach($data as $d) {
            if ($i == 0){continue;}
            $i++;
            $fields[] = strtolower(str_replace(" ", "_", $d));
        }
    }

Upvotes: 0

Related Questions