本文介绍了用PHP强制下载的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了一个PHP脚本来强制将文件下载给用户。一切正常,但有一个例外,文件未完全下载,假设有10 MB的mp3文件下载,但5 MB后停止并显示下载完成。
I have written a PHP script to force download files to users. Everything works great with one exception, the file is not completely downloaded let's say there is a 10 MB mp3 file downloading, but it stops after 5 MB and displays download completed.
我的网站托管者是godaddy.com
并且代码在下面
My site host is godaddy.com
And the code is below
<?php
if(ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off');
}
if ( isset ( $_REQUEST['file_name'] )) {
$filename = basename($_REQUEST['file_name']);
$filesize = filesize("mp3gallery/".$_REQUEST['file_name']);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-length: $filesize");
header("Content-Transfer-Encoding: binary");
header('Content-type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3');
header('Content-Disposition: attachment; filename="'.$filename.'"');
$read_file = "mp3gallery/".$_REQUEST['file_name'];
function readfile_chunked($filename,$retbytes=true) {
$chunksize = 1*(1024*1024); // how many bytes per chunk
$buffer = '';
$cnt =0;
// $handle = fopen($filename, 'rb');
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
echo $buffer;
ob_flush();
flush();
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
readfile_chunked($read_file,true);
}
?>
请帮助.........
Please Help.........
推荐答案
问题可能是您的脚本超时。
The problem is probably that your script times out.
那您该怎么办?
- 更改时间限制(可能无法执行,因为您的主机不允许您这样做)
- 加快速度
关于后者:是否有特定的原因导致您不仅仅使用?我希望它具有更好的性能。
Regarding the latter: is there a specific reason why you don't just use fpassthru
? I'd expect it to have better performance.
这篇关于用PHP强制下载的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!