0

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.

2
  • Just a quick note: you should never trust $_FILES["file"]["name"] without much more checking than shown here. It's far better to just generate a random filename, in a specific directory, so the user can't craft a filename that will mess something up. Commented Apr 24, 2013 at 20:43
  • you haven't stated what you're using the CSV data for once it's loaded into the php program, but if the ultimate goal is to load it into a mySQL database (which is a pretty common thing to want to do), you might want to look up mySQL's LOAD DATA INFILE command, which allows mysql to load a CSV file directly without needing any PHP code (other than one line to call the DB query). It has an option to skip the first line if necessary. Commented Apr 24, 2013 at 20:45

4 Answers 4

1

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........ !!!

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Hey, thanks for the answer, this doesn't work it just returns no values? Any other Ideas?
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.