PHP:强制下载标题不会显示总大小和速度

时间:2012-11-30 23:56:49

标签: php download

每当我使用此脚本下载文件时,我无法在下载时看到总大小和速度...我想让它看起来更像是“直接下载链接”。此脚本的目的是隐藏直接下载链接限制直接下载和其他下载行为,如机器人。想想mediafire,rapidshare,megaupload等。

我们现在可以使用的脚本但是没有显示从正常下载链接下载时的显示方式,我将发布正在发生的事情的屏幕截图:
enter image description here

我希望这个截图有所帮助,因为我已经在互联网上搜索了几个小时,似乎无法找到解决方案:(。

if (isset($_GET['file'])){
   $file = $_GET['file'];
   $path = '/home/user/domains/domain.com/files/upload/';
   $filepath = $path.$file;

   if (file_exists($filepath)){

    set_time_limit(0); // for slow connections

    header('Content-Description: File Transfer');
    header("Content-Disposition: attachment; filename=\"$file\"");
    header('Content-Type: application/octet-stream');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . filesize($filepath));
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Expires: 0');

    readfile($filepath); // send file to client 
   } 
   else{
    header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404); 
   }
  }else{
   header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404); 
  }

2 个答案:

答案 0 :(得分:2)

使用mod_deflate或类似内容在服务器级别压缩内容吗?

这里已经回答: Sending correct file size with PHP download script

  

“如果使用Zlib压缩文件,mod_deflate等内容长度标题将不准确,因此下载文件时最终会看到”未知大小“和”未知剩余时间“。”

     

“您可以使用适用的.htaccess文件中的以下行轻松地为单个脚本禁用它:

     

SetEnvIfNoCase Request_URI ^ / download.php no-gzip不变   其中download.php在这里假定位于服务器根目录路径中的下载脚本中(例如www.crimsonbase.com/download.php)。 (那是因为正则表达式是^ / download.php。)“

另外,请注意您的脚本不安全。有人可以有效地为_GET ['file']

发送以下get参数
../../../../../Documents/MyStuff

它将完全覆盖你的$ path限制。

建议删除路径中的任何..引用。

答案 1 :(得分:0)

我今天遇到了类似的问题,我无法获得下载文件的总大小(下载进度)。

浏览器标题指示“gzip”作为内容类型,我能够通过阻止服务器在download.php脚本上使用gzip来解决这个问题,通过这个htaccess行:

  

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

相关问题