开发者

Sporadic error downloading data from PHP application

A PHP application is offering binary data as a download:

header("Content-Type: application/octet-stream");       
header("Pragma: public");
header("Cache-Control: private");   
header("Content-Disposition: attachment; filename=\"$filename\"");
header("expires: 0");      
set_time_limit(0);
ob_clean();
flush();    
@readfile($completefilename); exit;

$completefilename is a stream like "ftp://user:pwd@..."

The size of the data can be several MByte. It works fine, but sp开发者_StackOverflow社区oradically I get the following error:

Sporadic error downloading data from PHP application


It's most likely that the remote stream is occasionally down, or times out.

Also as @fab says it could be that the file you are trying to load is larger than your script's memory.

You should start logging the errors readfile() returns, e.g. using the error_log php.ini directive.

If this needs to be completely foolproof, I think you'll have to use something more refined than readfile() that allows to set a timeout (like curl, or readfile with stream context options).

You could then catch any errors that occur while downloading, and serve a locally hosted fallback document instead. That document could e.g. be a text file containing the message "Resource xyz could not be loaded".


Do you have anything in your error logs? Maybe PHP is running out of memory because readfile() needs to pull the while file into memory. Make sure memory_limit is larger than the largest file you work on with readfile(). Another option is to output the file in chunks using fread().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜