Search code examples
phpphp4

fgetcsv returning NULL in PHP 4


One of our systems is running on PHP 4 and no I can't change that.

The fgetcsv function seems to return null no matter what file I upload.

Very simply put:

$handle = fopen($file,"r");
var_dump(fgetcsv($handle));
fclose($handle);

This will print out "NULL".

Doing var_dump on the $handle object does give me a resource:

resource(33) of type (stream)

But I just get NULL when using fgetcsv

I can get the contents of the file using file_get_contents, but then it's more awkawrd to parse it as a csv.

As I say, I can't really do anything about it being on PHP 4. Does anyone know what might be causing this, or shall I find another way?

Thanks


Solution

  • Your original issue may be related to temporary uploaded file usage.
    Try to open it after move_uploaded_file

    Also, fseek($handle, 0) can help theoretically, because it was read already anywhere.


    I can get the contents of the file using file_get_contents

    You can try to use tmpfile then:

    $csv = file_get_contents($file);
    $temp = tmpfile();
    fwrite($temp, $csv);
    fseek($temp, 0); // prepare for read at start
    $data = fgetcsv($temp);
    fclose($temp); // file autoremoved here