PHP下载脚本在文件下载过程中不会显示文件大小。

3
我正在使用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>

1
请确保将答案标记为正确的答案。 - Christian
1个回答

2
我读到Apache的“mod_deflate”扩展可能会导致这种类型的脚本出现问题。如果您启用了该扩展,应该为该特定脚本禁用它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接