Вопрос

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?

Это было полезно?

Решение

You have 2 options:

Comment this line:

set_time_limit(0);

Disable the safe mode.

Другие советы

You've prbably got something unwanted in an output buffer.

Include

flush();
ob_clean();

before you start your download, just to make sure everything is clean.

set_time_limit has no effect in safe mode, and that warning slips into your output spoiling it. See http://www.php.net/manual/en/features.safe-mode.functions.php and http://php.net/manual/en/features.safe-mode.php

Try to switch safe mode off or don't use set_time_limit and try to extend the default run time in php.ini, see http://www.php.net/manual/en/function.set-time-limit.php

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top