在下载文件时,文件大小不会显示在PHP下载脚本中 [英] File size will not display on PHP download script during file download

查看:234
本文介绍了在下载文件时,文件大小不会显示在PHP下载脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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);
退出;
}
else {
die;
}

提前感谢





如果有人绊倒,需要弄清楚如何修复将它添加到.htaccess中。

 #以/ foo / bar /开头的URL路径
SetEnvIf Request_URI ^ / foo / bar / no-gzip = 1

#以.py结尾的文件
< FilesMatch \.py $>
SetEnv no-gzip 1
< / FilesMatch>


解决方案

我看到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.

这篇关于在下载文件时,文件大小不会显示在PHP下载脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆