phpdownloadphp-safe-mode

PHP force download results in corrupted file


I've got a PHP script that forces a download. Heres my code

//$file and $mime have been set earlier
$basename = basename($file);
$length   = sprintf("%u", filesize($file));
header('Content-Description: File Transfer');
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="' . $basename . '"');
header('Content-Transfer-Encoding: Binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $length);
set_time_limit(0);
readfile($file);

Now this script works perfectly on my local server, but when I uploaded it to my site, and tried it out, the file downloaded (To test it i used an image), but when I opened it, i got Windows Photo Viewer can't open this picture because the file appears to be damaged, corrupted, or is too large.

I opened the file in Sublime Text, here's what it says
Warning: set_time_limit() [function.set-time-limit]: Cannot set time limit in safe mode in /mounted-storage/home61c/sub001/sc38639-USWQ/www/test/scripts/download.php on line 32
‰PNG

bla bla bla (I'm unable to copy and paste the stuff below it)

What's going on?


Solution

  • You have 2 options:

    Comment this line:

    set_time_limit(0);
    

    Disable the safe mode.