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

文件下载期间,PHP下载脚本上不会显示文件大小,php,download,filesize,content-length,Php,Download,Filesize,Content Length,当你进入一个页面时,我使用一个PHP脚本来下载一个文件,但是我不能设置它,这样当一个人下载文件时,它会显示文件大小(我的显示“未知剩余时间”)。我在谷歌上到处找了找,找不到如何让文件大小显示给我,因为根据所有这些脚本,我做对了。我对文件大小进行了回显,以确保脚本正确读取文件,并以字节为单位返回正确的文件大小 $file = 'audio/song.mp3'; if(file_exists($file)) { header('Content-description: File Trans

当你进入一个页面时,我使用一个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>
#用于以“/foo/bar/”开头的URL路径
SetEnvIf请求_URI^/foo/bar/no gzip=1
#对于以“.py”结尾的文件
SetEnv编号gzip 1

我了解到Apache的“mod_deflate”扩展可能会导致此类脚本出现问题。如果已启用,则应针对特定脚本禁用它。

确保将答案标记为正确答案。