当您转到页面时,我正在使用 PHP 脚本下载文件,但我无法将其设置为当有人下载文件时,它会显示文件大小(我的显示“剩余时间未知”)。我在谷歌上环顾四周,找不到如何让文件大小显示给我,因为根据所有这些脚本,我做对了。我已经回显了文件大小以确保脚本正确读取文件,并返回正确的文件大小(以字节为单位)。
$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>