Search code examples
phpfgetcsv

PHP - fgetcsv() reset pointer


How do I reset the file pointer with fgetcsv()?

I have the loop below, nested inside another while loop. However it starts reading the CSV from the break point rather than the beginning after first run.

while($line2 = fgetcsv($fp2, $length2, $fildDelineate2)) {
        if($line[0] == $line2[0]) {
            $desc = addslashes(strip_tags($line2[6]));
            $import2 = "insert into $table2 values('$id','1',\"$line[1]\",'$desc','','0')";
            mysql_query($import2) or die('import2 '.mysql_error());
            $count++;
            break;
        }
}

Solution

  • You can use rewind() or fseek() to do this:

    rewind($fp2);
    // ...is the same as:
    fseek($fp2, 0);