PHP readfile returns file with zero length

This is weird .

I have a script that sends local zip files to a user through a browser. The script works fine so far without any problems. Today my colleague informed me that the script is sending files of zero length.

Some background information:

  • Server settings were not changed before the script went wrong.
  • Various browsers checked (same in Chrome / Firefox)
  • Previous zip files (which worked just fine before) also have zero length.
  • Script finds files on the server
  • Is the file size correct (when echoing for debugging)
  • I tried to configure the server and script settings as here without success.

UPDATE:

  • is_readable() returns 1
  • file size can vary from 5 MB to 100 MB (undefined)
  • $zip_file contains file path
  • $zip_name contains the name zip
  • the file really has zero length (opened in a text editor, it does not contain one byte)
  • error_reporting On( E_ALL) shows nothing
  • without headers, the browser correctly displays the source "zip"
  • Safari says: " 0 bytes? Can't decode source data 'first useful (?) Symptom

The fragment in question:

if (file_exists($zip_file)) {
    header('Content-type: application/zip');
    header('Content-disposition: filename="' . $zip_name . '"');
    header("Content-length: " . filesize($zip_file));
    readfile($zip_file);
    exit();
}

How can I easily debug this?

Thanks in advance, fabrik

+5
source share
4 answers

My. Lame. Error. Sorry for everyone.

Just updated the code a week ago and added:

if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
    ob_start('ob_gzhandler');
} else {
    ob_start();
}

What caused this anomaly :(

0
source

http://www.php.net/manual/en/function.readfile.php#102137:

, :

header('Content-Length: ' . filesize($file));

$file . , "0 ".

+5

The biggest problem I think of is how you send the file, did you try to send it to pieces:

if (file_exists($zip_file))
{
    header('Content-type: application/zip');
    header('Content-disposition: filename="' . $zip_name . '"');
    header("Content-length: " . filesize($zip_file));

    $resource = fopen($zip_file,'r');
    while(!feof($resource))
    {
         $chunk = fread($resource,4096);
         //....
         echo $chunk;
    }

    exit();
}
+1
source

Try adding attachment;and using a different browser.

header('Content-disposition: attachment; filename="' . $zip_name . '"');
0
source

All Articles