Helo,我有这样的代码强制通过URL(远程)从另一台服务器下载文件。
$file_size = get_headers($url,TRUE);
header('Pragma: public'); // required
header('Accept-Ranges: bytes');
header('Access-Control-Allow-Headers: accept, authorization');
header('Access-Control-Allow-Methods: GET, HEAD');
header('Access-Control-Allow-Origin: *');
header('Content-Type: audio/mpeg');
header("Content-Disposition: attachment; filename=\"myfile.ogg\";" );
//header('Content-Transfer-Encoding: binary');
header('X-Cache: HIT');
header('Content-Length: '.$file_size['Content-Length']);
readfile($url);
这个PHP代码在PC浏览器上运行良好(我正在使用Ubuntu 14.04和谷歌Chrome浏览器)。也适用于wget,并通过curl下载。
但是,当我从我的手机下载(Android CyanogenMod ROM 4.4.4与谷歌浏览器浏览器和UC网络浏览器)时,它总是说在下载时不成功(例如:当进度条达到30%时,下载失败)。
当我尝试检查标题时,我正在使用codeigniter创建整个网络,这就是结果。
HTTP/1.1 200 OK
Date: Fri, 20 Mar 2015 01:08:13 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.6
Set-Cookie: lagu_session=a%3A5%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22c0bf700656a88ff98ddce8098c7ea1e3%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A9%3A%22127.0.0.1%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A11%3A%22curl%2F7.35.0%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1426813694%3Bs%3A9%3A%22user_data%22%3Bs%3A0%3A%22%22%3B%7D2c0017d813b89cb687b243a8a4c73771fe1c9083; expires=Fri, 20-Mar-2015 03:08:14 GMT; Max-Age=7200; path=/
Pragma: public
Accept-Ranges: bytes
Access-Control-Allow-Headers: accept, authorization
Access-Control-Allow-Methods: GET, HEAD
Access-Control-Allow-Origin: *
Content-Disposition: attachment; filename="myfile.ogg";
X-Cache: HIT
Content-Length: 4017422
Content-Type: audio/mpeg
有什么问题,有什么想法吗?
这条线会导致问题吗?
Set-Cookie: lagu_session=a%3A5%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22c0bf700656a88ff98ddce8098c7ea1e3%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A9%3A%22127.0.0.1%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A11%3A%22curl%2F7.35.0%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1426813694%3Bs%3A9%3A%22user_data%22%3Bs%3A0%3A%22%22%3B%7D2c0017d813b89cb687b243a8a4c73771fe1c9083; expires=Fri, 20-Mar-2015 03:08:14 GMT; Max-Age=7200; path=/
仅供参考:当我的朋友使用他们的手机下载时,这没有错误。我手机上的这个问题?我正在尝试从其他网站下载一些文件,这对我的手机没问题。
感谢您的任何建议和解决方案:D
修改 我认为这是服务器问题,我正在使用openshift(免费计划)并解决了这个问题。现在我将我的网络应用程序移动到DigitalOcean,它运行良好。
答案 0 :(得分:1)
最后,我发现了问题,没有关于服务器的问题。这是我的代码片段
//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)) ? ($header_content_size - 1) : min(abs(intval($seek_end)),($header_content_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 < ($header_content_size - 1))
{
header('HTTP/1.1 206 Partial Content');
header('Content-Range: bytes '.$seek_start.'-'.$seek_end.'/'.$header_content_size);
header('Content-Length: '.($seek_end - $seek_start + 1));
}else{
header("Content-Length: $header_content_size");
}
header('Accept-Ranges: bytes');
@set_time_limit(0);
fseek($file, $seek_start);
while(!feof($file))
{
print(@fread($file, 1024*8));
ob_flush();
flush();
if (connection_status()!=0)
{
@fclose($file);
exit;
}
}
// file save was a success
@fclose($file);
exit;
现在,我的下载脚本支持可重复下载。