3

当您转到页面时,我正在使用 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>
4

1 回答 1

2

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

于 2011-04-05T05:18:17.443 回答