且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在下载文件时,文件大小不会显示在PHP下载脚本中

更新时间:2023-11-08 10:56:22

我看到Apache的mod_deflate扩展名可能会导致问题用这种类型的脚本。如果您启用该功能,则应禁用该脚本。


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$>
    SetEnv no-gzip 1
</FilesMatch>

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.