我正在使用PHP脚本下载文件,但我无法设置当一个人下载文件时,它会显示文件大小(我的显示“未知剩余时间”)。我在Google上搜索了很久,但找不到让文件大小显示的方法,因为根据所有这些脚本的说法,我已经做得没问题了。我已经echo'd文件大小以确保脚本正常读取文件,并且以字节为单位返回了正确的文件大小。
$file = 'audio/song.mp3';
if(file_exists($file)) {
header('Content-description: File Transfer');
header('Content-type: application/force-download');
header('Content-disposition: attachment; filename="' . $songname . '_' . $filetype . '.' . $ext . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
else {
die;
}
提前感谢您。
感谢您,禁用mod_deflate起了作用。
如果有人偶然遇到这个问题需要解决,将以下代码添加到您的.htaccess文件中即可。
# for URL paths that begin with "/foo/bar/"
SetEnvIf Request_URI ^/foo/bar/ no-gzip=1
# for files that end with ".py"
<FilesMatch \.py$>
SetEnv no-gzip 1
</FilesMatch>