File size will not display on PHP download script during file download

余生长醉 提交于 2019-12-10 20:19:13

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/5547471/file-size-will-not-display-on-php-download-script-during-file-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!