php下载失败在下载
我使用这个脚本来下载php文件。
Here是我复制脚本的原始源代码。php下载失败在下载
if(file_exists($path)){
$file_size = filesize($path);
$file = fopen($path,"rb");
$chunksize = 2*1024*1024; // how many bytes per chunk
if($file){
header("Content-Description: File Transfer");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0");
header("Content-Disposition: attachment; filename=\"$name.$format\"");
header("Content-Type: $mimetype");
//check if http_range is sent by browser (or download manager)
if(isset($_SERVER['HTTP_RANGE'])){
list($size_unit, $range_orig) = explode('=', $_SERVER['HTTP_RANGE'], 2);
if ($size_unit == 'bytes'){
//multiple ranges could be specified at the same time, but for simplicity only serve the first range
//http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt
list($range, $extra_ranges) = explode(',', $range_orig, 2);
}
else{
$range = '';
header('HTTP/1.1 416 Requested Range Not Satisfiable');
exit;
}
}
else{
$range = '';
}
//figure out download piece from range (if set)
list($seek_start, $seek_end) = explode('-', $range, 2);
//set start and end based on range (if set), else set defaults
//also check for invalid ranges.
$seek_end = (empty($seek_end)) ? ($file_size - 1) : min(abs(intval($seek_end)),($file_size - 1));
$seek_start = (empty($seek_start) || $seek_end < abs(intval($seek_start))) ? 0 : max(abs(intval($seek_start)),0);
//Only send partial content header if downloading a piece of the file (IE workaround)
if ($seek_start > 0 || $seek_end < ($file_size - 1)){
header('HTTP/1.1 206 Partial Content');
header('Content-Range: bytes '.$seek_start.'-'.$seek_end.'/'.$file_size);
header('Content-Length: '.($seek_end - $seek_start + 1));
}
else
header("Content-Length: $file_size");
header('Accept-Ranges: bytes');
set_time_limit(0);
@fseek($file, $seek_start);
while(!feof($file)){
print(@fread($file, $chunksize));
ob_flush();
flush();
if (connection_status()!=0){
@fclose($file);
exit;
}
}
@fclose($file);
exit;
}
else{
// file couldn't be opened
header("HTTP/1.0 500 Internal Server Error");
exit;
}
}
else {
// file does not exist
header("HTTP/1.0 404 Not Found");
exit;
}
现在,问题所在。
下载大文件(例如g> 20MB)有时下载成功完成。有时会在下载过程中中断。例如我测试并发现,对于100MB文件,在下载40MB后失败。 (不是一次,多次)。
在浏览器中,似乎文件完全下载。 (Firefox和chrome在下载栏中没有失败的文本,但存在“打开包含文件夹”选项)
我的一些网络连接速度很慢的朋友说,他发生的小问题(少于1 MB )。但我从未体验过它。 (也许是因为我的网络速度不慢)
所以问题是,当一个连接速度慢的人想要下载,在中间,它会打破他。 (我想服务器不发送文件给他了。)
注1:我有两个头太:
Connection: Keep-Alive
Keep-Alive: timeout=5
这是任何一个例子下载文件谁想要测试一下: https://goo.gl/9flVng
还有一个问题,发送用户直接下载文件header: location
而不是readfile(), fread(), ...
是不是更好?
对不起长问题,谢谢你的帮助。
您可以尝试提高脚本的最大执行时间限制,默认情况下它设置为30秒。将其设置为0可消除时间限制。
set_time_limit (0)
谢谢路易斯的帮助。是的,我已经做到了。 你可以在我提供的代码中看到它。但没有积极的结果。 任何其他帮助? 我想问题是与服务器或相关的东西,而不是脚本。任何想法? – Saeed
可能值得考虑cdn作为另一个选项 – Adam
耶,cdn是一个好主意。我还没有经历过,似乎我应该测试一下。但关于目前的问题,我的服务器是一个很好的问题。不应该有任何问题。任何关于脚本或服务器配置的想法? 我只知道客户端和服务器之间的连接拒绝并且不会重新连接。 为什么浏览器不显示“失败”文本? – Saeed