我在使用PHP下载mp3文件时遇到问题。我需要下载该文件,并对其进行重命名。下面是它包含以下代码的download.php文件:重命名mp3文件并在PHP中下载文件
$file = $_GET['file'];
$flname = explode("/",$file);
$num = sizeof($flname);
$filenme = $flname[$num-1];
$name_of_file = $filenme;
header('Content-Description: File Transfer');
header('Content-type: application/mp3');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: '.filesize($name_of_file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
readfile_chunked($file);
function readfile_chunked($file)
{
$chunksize = 1*(1024*1024); // how many bytes per chunk
$buffer = '';
$handle = fopen($filename, 'rb');
if ($handle === false)
{
return false;
}
while (!feof($handle))
{
$buffer = fread($handle, $chunksize);
print $buffer;
}
return fclose($handle);
}
我得到在$file
文件路径:
$file = $_GET['file'];
其中$file
变为:
$file = "localhost/project/mp3 file path";
然后我爆炸它只获取mp3文件名(从而删除路径)。 我不知道问题是什么,但即使文件大小为1-2MB,它在Firefox的下载对话框中也总是显示大约490个字节。有人能解释我做错了什么吗?
预先感谢您。
尝试更改Content-Type,请参阅示例http://stackoverflow.com/questions/12017694/content-type-for-mp3-download-response –