发送正确的文件大小与PHP脚本下载 [英] Sending correct file size with PHP download script

查看:115
本文介绍了发送正确的文件大小与PHP脚本下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中创建的文件下载脚本,它的工作原理,但网页浏览器报告文件为未知长度。我的code是如下:

 函数downloadFile($文件){
  //设置下载系统...
  标题(内容说明:文件传输');
  标题(内容类型:.mime_content_type($文件));
  标题(内容处置:附件;文件名='基名($文件)。'');
  标题(内容传输编码:二进制');
  头('过期:0');
  标题(缓存控制:必重新验证,检查后= 0,pre-检查= 0');
  头('杂注:公开');
  标题(内容长度:.filesize($文件));  //清空缓存
  ob_clean();
  冲洗();  //发送文件到浏览器
  ReadFile的($文件);  //什么也不做后文件下载
  出口;
}


解决方案

http://paul.luminos.nl/update本来/ 471 :


  

借助 CrimsonBase网站通过并使它们通过一个类似于由安德鲁·约翰逊的his文章关于PHP控制文件下载


  
  

安德鲁使得一个非常重要的意见在文章的结尾:


  
  

    

如果你的COM preSS与Zlib压缩,mod_deflate模块等等Content-Length头将不准确,所以你最终看到的文件未知大小和未知剩余时间下载文件时。


  
  
  

我想强调这一点:如果你的浏览器不似乎服从你的PHP生成的头脚本,特别是的Content-Length - 它是相当可能的阿帕奇的 mod_deflate模块扩展功能。


  
  

您可以用下面的行适用的的.htaccess 文件轻松地禁用它为一个脚本:

  SetEnvIfNoCase REQUEST_URI ^ /下载\\ .PHP无gzip的不要,各不相同


  
  

在这里的download.php这里假设是在位于服务器的根目录路径的下载脚本(例如 www.crimsonbase.com/download.php )。 (这是因为常规的前pression是 ^ /下载\\的.php


I created a file download script in PHP, it works, but web browsers report the file as "Unknown Length". My code is as follows:

function downloadFile($file){
  // Set up the download system...
  header('Content-Description: File Transfer');
  header('Content-Type: '.mime_content_type($file));
  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));

  // Flush the cache
  ob_clean();
  flush();

  // Send file to browser
  readfile($file);

  // DO NOT DO ANYTHING AFTER FILE DOWNLOAD
  exit;
}

解决方案

Originally from http://paul.luminos.nl/update/471:

The CrimsonBase website verifies downloads by passing them through a robust PHP script similar to the one published by Andrew Johnson in his article about PHP-controlled file downloads.

Andrew makes a very important comment at the end of the article:

"If you compress files with Zlib, mod_deflate and so on the Content-Length header won't be accurate so you'll end up seeing "Unknown size" and "Unknown time remaining" when downloading files."

I would like to stress this: if your browser doesn't appear to be obeying the headers generated by your PHP script—especially Content-Length—it is fairly likely that Apache's mod_deflate extension is enabled.

You can easily disable it for a single script using the following line in an applicable .htaccess file:

SetEnvIfNoCase Request_URI ^/download\.php no-gzip dont-vary

where download.php is here assumed to be in the download script located in the server's root directory path (e.g. www.crimsonbase.com/download.php). (That's because the regular expression is ^/download\.php.)

这篇关于发送正确的文件大小与PHP脚本下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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