文件下载期间 PHP 下载脚本中不会显示文件大小

发布于 2024-10-30 10:32:22 字数 1080 浏览 2 评论 0原文

我正在使用 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 \.py
gt;
    SetEnv no-gzip 1
</FilesMatch>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有你我更好 2024-11-06 10:32:22

我读到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文