dongpu7881 2014-07-20 20:58
浏览 149
已采纳

如何在强制浏览器下载PHP时显示文件大小

I'm downloading mp4,mp3 etc.. files from my server with php. But browsers are not showing the size of file. I searched everywhere but i couldn't find any working answer. Hope you can help me. Thanks in advance.

Here is the code :

  function force_download($file)
 {
 $ext = explode(".", $file);
 switch($ext[sizeof($ext)-1])
{
  case 'jar': $mime = "application/java-archive"; break;
  case 'zip': $mime = "application/zip"; break;
  case 'jpeg': $mime = "image/jpeg"; break;
  case 'jpg': $mime = "image/jpg"; break;
  case 'jad': $mime = "text/vnd.sun.j2me.app-descriptor"; break;
  case "gif": $mime = "image/gif"; break;
  case "png": $mime = "image/png"; break;
  case "pdf": $mime = "application/pdf"; break;
  case "txt": $mime = "text/plain"; break;
  case "doc": $mime = "application/msword"; break;
  case "ppt": $mime = "application/vnd.ms-powerpoint"; break;
  case "wbmp": $mime = "image/vnd.wap.wbmp"; break;
  case "wmlc": $mime = "application/vnd.wap.wmlc"; break;
  case "mp4s": $mime = "application/mp4"; break;
  case "ogg": $mime = "application/ogg"; break;
  case "pls": $mime = "application/pls+xml"; break;
  case "asf": $mime = "application/vnd.ms-asf"; break;
  case "swf": $mime = "application/x-shockwave-flash"; break;
  case "mp4": $mime = "video/mp4"; break;
  case "m4a": $mime = "audio/mp4"; break;
  case "m4p": $mime = "audio/mp4"; break;
  case "mp4a": $mime = "audio/mp4"; break;
  case "mp3": $mime = "audio/mpeg"; break;
  case "m3a": $mime = "audio/mpeg"; break;
  case "m2a": $mime = "audio/mpeg"; break;
  case "mp2a": $mime = "audio/mpeg"; break;
  case "mp2": $mime = "audio/mpeg"; break;
  case "mpga": $mime = "audio/mpeg"; break;
  case "wav": $mime = "audio/wav"; break;
  case "m3u": $mime = "audio/x-mpegurl"; break;
  case "bmp": $mime = "image/bmp"; break;
  case "ico": $mime = "image/x-icon"; break;
  case "3gp": $mime = "video/3gpp"; break;
  case "3g2": $mime = "video/3gpp2"; break;
  case "mp4v": $mime = "video/mp4"; break;
  case "mpg4": $mime = "video/mp4"; break;
  case "m2v": $mime = "video/mpeg"; break;
  case "m1v": $mime = "video/mpeg"; break;
  case "mpe": $mime = "video/mpeg"; break;
  case "mpeg": $mime = "video/mpeg"; break;
  case "mpg": $mime = "video/mpeg"; break;
  case "mov": $mime = "video/quicktime"; break;
  case "qt": $mime = "video/quicktime"; break;
  case "avi": $mime = "video/x-msvideo"; break;
  case "midi": $mime = "audio/midi"; break;
  case "mid": $mime = "audio/mid"; break;
  case "amr": $mime = "audio/amr"; break;
  default: $mime = "application/force-download";
 }
header('Content-Description: File Transfer');
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename='.basename($file));
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);
}   

if (isset($_POST['downl'])){
$file = "http://localhost/project1/downloads/gmf.mp4";
force_download($file); 

}
<form  method="POST" action=''>
    <?php echo'<input type="submit" name="downl"  value="Download Baby" >' ;    ?>
</form>

If i try to show file size with echo filesize($file) it gives this error :

 Warning: filesize(): stat failed for http://localhost/project1/downloads...
  • 写回答

1条回答 默认 最新

  • duanqi6274 2014-07-20 21:04
    关注

    Since php 5.0.0 the file command supports http wrappers but the http wrapper does not support stat commands like size. So you cannot get the file size in your case with size.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?