Asked  2 Years ago    Answers:  5   Viewed   130 times

I am trying to import a CSV file. Due to the program we use, the first row is basically all headers that I would like to skip since I've already put my own headers in via HTML. How can I get the code to skip the first row of the CSV? (the strpos command is to cut off the first field in all the rows.)

$row = 1;
if (($handle = fopen("ptt.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    for ($c=0; $c < $num; $c++) {
    if(strpos($data[$c], 'Finished') !== false) {
echo "<TR> <TD nowrap>" . $data[$c] . "</ TD>"; }
        echo "<TD nowrap>" .  $data[$c] . "</ TD>";



As you are keeping track of the row number anyway, you can use continue to skip the rest of the loop for the first row.

For example, add this at the start of your while loop (just above $num = count($data)):

if($row == 1){ $row++; continue; }

There are other ways to do this, but just make sure that when you continue, $row is still being incremented or you'll get an infinite loop!

Sunday, December 4, 2022

OK, solved.

This is what everyone was suspecting: the encoding of the file was messed up. I could not know which encoding this was, but LibreOffice proposed me Unicode whenever I tried to open the CSVs.

I had to open them with nano to realize there was indeed an encoding problem. Gedit, vim or any other tool I had on my computer raised no errors. When opened with nano, an @ symbol was inserted between every other characters and line feeds were not read correctly.

It seems there are some encodings that are not well supported by fgetcsv. To solve the problem, I recreated the files from nano (copy-paste from another tool that did not display the @).

Thursday, December 15, 2022

This is specific to the Mac version of Excel. Line breaks are handled differently on Macs for some arbitrary reason, so before using fgetcsv, you should do this;

Friday, October 21, 2022

You can delete the first line of a file using sed:

sed -i '' 1d file.csv

If you just want to get the contents of the file without the first line, and without modifying the file, remove the -i '' flag:

sed 1d file.csv

Here 1d is the command to execute:

  • 1 => line where to act
  • d => delete

So 1d means 'delete line 1'

If you want to get the first line, you can use sed too:

sed -n 1p file.csv

Here p stands for 'print' or

sed 1q file.csv

(see William Pursell's comment)

Wednesday, December 14, 2022

Building on Nouveau's code you could do this

for($i = 0; $i <= count($fieldList); $i++)
    if (is_numeric($fieldList[$i]))
        if (strpos($fieldList[$i],'.') !== false){
            $fieldList[$i] = (int)$fieldList[$i];
            $fieldList[$i] = (float)$fieldList[$i];

    switch(gettype($fieldList[$i])) {
        case 'integer':
            $typeInfo = 'int(11)';
        case 'float':
        case 'double':
            $typeInfo = 'float';

        case 'string':
            $typeInfo = 'varchar(80)';
            $typeInfo = 'varchar(80)';
if(gettype($fieldList[$i]) != NULL) echo "t".'`'.$i.'` '.$typeInfo.' NOT NULL, --'.gettype($fieldList[$i]).' '.$fieldList[$i]."<br/>n";


That works, note the addition of "case 'double':" in the switch, but there may be a better way to do the int/float check as they would only work with standard uk/us numbers.

Friday, August 26, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages