文件下载期间 PHP 下载脚本中不会显示文件大小
我正在使用 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>
I am using a PHP script to download a file when you go to a page, but I can't set it up so that when a person downloads the file, it will show the file size (mine shows "unknown time remaining"). I've looked around on Google and can't find out how to make file sizes show up for me, because according to all these scripts I'm doing it right. I have echo'd the filesize to make sure that the script is reading the file properly, and it returns the correct file size in bytes.
$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;
}
Thanks in advance.
Thank you, that disabling mod_deflate worked.
In case anyone stumbles here and needs to figure out how to fix it, add this to your .htaccess.
# for URL paths that begin with "/foo/bar/"
SetEnvIf Request_URI ^/foo/bar/ no-gzip=1
# for files that end with ".py"
<FilesMatch \.pygt;
SetEnv no-gzip 1
</FilesMatch>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我读到 Apache 的“mod_deflate”扩展可能会导致此类脚本出现问题。如果您启用了它,则应该针对该特定脚本禁用它。
I read that Apache's "mod_deflate" extension can cause problems with this type of script. If you have it enabled, you should disable it for that particular script.