【问题标题】:Is there a good implementation of partial file downloading in PHP?在 PHP 中是否有一个很好的部分文件下载实现?
【发布时间】:2009-09-08 18:47:47
【问题描述】:

我正在编写一个允许用户下载文件的 PHP 脚本。基本上这个想法是为了防止文件被下载超过 X 次,因为它是付费内容,并且链接不应该传播。

由于文件会很大,所以最好实现恢复。我已经阅读了the standard,但它很长并且具有一定的灵活性。由于我需要快速完成它,因此我更喜欢此功能的稳定且经过测试的实现。

谁能指点我这样一个脚本?

【问题讨论】:

    标签: php download resume


    【解决方案1】:

    似乎我找到了自己需要的东西。这样其他人可能会从中受益,这是链接:http://www.coneural.org/florian/papers/04_byteserving.php

    为了防止原始页面停止工作(脚本已经很老了),这里是它的副本:

    <?php 
    /*
    
    The following byte serving code is (C) 2004 Razvan Florian. You may find the latest version at 
    http://www.coneural.org/florian/papers/04_byteserving.php
    
    */
    function set_range($range, $filesize, &$first, &$last){
      /*
      Sets the first and last bytes of a range, given a range expressed as a string 
      and the size of the file.
    
      If the end of the range is not specified, or the end of the range is greater 
      than the length of the file, $last is set as the end of the file.
    
      If the begining of the range is not specified, the meaning of the value after 
      the dash is "get the last n bytes of the file".
    
      If $first is greater than $last, the range is not satisfiable, and we should 
      return a response with a status of 416 (Requested range not satisfiable).
    
      Examples:
      $range='0-499', $filesize=1000 => $first=0, $last=499 .
      $range='500-', $filesize=1000 => $first=500, $last=999 .
      $range='500-1200', $filesize=1000 => $first=500, $last=999 .
      $range='-200', $filesize=1000 => $first=800, $last=999 .
    
      */
      $dash=strpos($range,'-');
      $first=trim(substr($range,0,$dash));
      $last=trim(substr($range,$dash+1));
      if ($first=='') {
        //suffix byte range: gets last n bytes
        $suffix=$last;
        $last=$filesize-1;
        $first=$filesize-$suffix;
        if($first<0) $first=0;
      } else {
        if ($last=='' || $last>$filesize-1) $last=$filesize-1;
      }
      if($first>$last){
        //unsatisfiable range
        header("Status: 416 Requested range not satisfiable");
        header("Content-Range: */$filesize");
        exit;
      }
    }
    
    function buffered_read($file, $bytes, $buffer_size=1024){
      /*
      Outputs up to $bytes from the file $file to standard output, $buffer_size bytes at a time.
      */
      $bytes_left=$bytes;
      while($bytes_left>0 && !feof($file)){
        if($bytes_left>$buffer_size)
          $bytes_to_read=$buffer_size;
        else
          $bytes_to_read=$bytes_left;
        $bytes_left-=$bytes_to_read;
        $contents=fread($file, $bytes_to_read);
        echo $contents;
        flush();
      }
    }
    
    function byteserve($filename){
      /*
      Byteserves the file $filename.  
    
      When there is a request for a single range, the content is transmitted 
      with a Content-Range header, and a Content-Length header showing the number 
      of bytes actually transferred.
    
      When there is a request for multiple ranges, these are transmitted as a 
      multipart message. The multipart media type used for this purpose is 
      "multipart/byteranges".
      */
    
      $filesize=filesize($filename);
      $file=fopen($filename,"rb");
    
      $ranges=NULL;
      if ($_SERVER['REQUEST_METHOD']=='GET' && isset($_SERVER['HTTP_RANGE']) && $range=stristr(trim($_SERVER['HTTP_RANGE']),'bytes=')){
        $range=substr($range,6);
        $boundary='g45d64df96bmdf4sdgh45hf5';//set a random boundary
        $ranges=explode(',',$range);
      }
    
      if($ranges && count($ranges)){
        header("HTTP/1.1 206 Partial content");
        header("Accept-Ranges: bytes");
        if(count($ranges)>1){
          /*
          More than one range is requested. 
          */
    
          //compute content length
          $content_length=0;
          foreach ($ranges as $range){
            set_range($range, $filesize, $first, $last);
            $content_length+=strlen("\r\n--$boundary\r\n");
            $content_length+=strlen("Content-type: application/pdf\r\n");
            $content_length+=strlen("Content-range: bytes $first-$last/$filesize\r\n\r\n");
            $content_length+=$last-$first+1;          
          }
          $content_length+=strlen("\r\n--$boundary--\r\n");
    
          //output headers
          header("Content-Length: $content_length");
          //see http://httpd.apache.org/docs/misc/known_client_problems.html for an discussion of x-byteranges vs. byteranges
          header("Content-Type: multipart/x-byteranges; boundary=$boundary");
    
          //output the content
          foreach ($ranges as $range){
            set_range($range, $filesize, $first, $last);
            echo "\r\n--$boundary\r\n";
            echo "Content-type: application/pdf\r\n";
            echo "Content-range: bytes $first-$last/$filesize\r\n\r\n";
            fseek($file,$first);
            buffered_read ($file, $last-$first+1);          
          }
          echo "\r\n--$boundary--\r\n";
        } else {
          /*
          A single range is requested.
          */
          $range=$ranges[0];
          set_range($range, $filesize, $first, $last);  
          header("Content-Length: ".($last-$first+1) );
          header("Content-Range: bytes $first-$last/$filesize");
          header("Content-Type: application/pdf");  
          fseek($file,$first);
          buffered_read($file, $last-$first+1);
        }
      } else{
        //no byteserving
        header("Accept-Ranges: bytes");
        header("Content-Length: $filesize");
        header("Content-Type: application/pdf");
        readfile($filename);
      }
      fclose($file);
    }
    
    function serve($filename, $download=0){
      //Just serves the file without byteserving
      //if $download=true, then the save file dialog appears
      $filesize=filesize($filename);
      header("Content-Length: $filesize");
      header("Content-Type: application/pdf");
      $filename_parts=pathinfo($filename);
      if($download) header('Content-disposition: attachment; filename='.$filename_parts['basename']);
      readfile($filename);
    }
    
    //unset magic quotes; otherwise, file contents will be modified
    set_magic_quotes_runtime(0);
    
    //do not send cache limiter header
    ini_set('session.cache_limiter','none');
    
    
    $filename='myfile.pdf'; //this is the PDF file that will be byteserved
    byteserve($filename); //byteserve it!
    ?>
    

    【讨论】:

    • Vilx 答案代码非常适合我。我需要它在浏览器窗口中打开 PDF。现在它可以工作了。
    • 它适用于本地文件,但用于下载远程文件时,它无法生成可恢复的链接。
    • @SiyamakShahpasand - “下载远程文件”是什么意思?如果需要下载的文件不在您的服务器上,那么您没有下载 - 您正在重定向。
    • @Vilix - 我的意思是这个脚本无法将远程文件直接下载到具有恢复功能的客户端。
    • @SiyamakShahpasand - 我会重复我的问题。什么是“远程文件”?
    【解决方案2】:

    您应该使用 PEAR HTTP_Download。它非常易于使用,并且只允许下载恢复文件:

    http://pear.php.net/manual/en/package.http.http-download.intro.php

    【讨论】:

      【解决方案3】:

      基于此:

      http://w-shadow.com/blog/2007/08/12/how-to-force-file-download-with-php/

      (你也可以使用)

      我制作了一个小型库,可以完成 PECL http_send_file 扩展的功能:

      http://php.net/manual/en/function.http-send-file.php

      (你也可以使用)

      该库类似于 http_send_file,但如果您没有安装 PECL 库的选项,您可以使用 http-send-file 库:

      https://github.com/diversen/http-send-file

      【讨论】:

        【解决方案4】:

        http://us3.php.net/manual/en/function.fread.php

        另一种方法是让 Web 服务器可以通过重定向到相关文件来处理 http。

        PHP 脚本可以在调用 header("Location $urltofile"); 之前执行任何所需的检查(安全、身份验证、验证文件、增加下载计数)和任何其他任务。

        我用 apache 对此进行了测试。中断/恢复下载工作。服务器的 mime 类型配置将决定客户端的行为。对于 apache,如果 mime.types 中的默认值不合适,则 mod_mime 的配置指令可以放在要下载的文件目录中的 .htaccess 文件中。如果真的有必要,这些甚至可以由 PHP 脚本在重定向之前编写。

        【讨论】:

        • header('Location:') 的想法很糟糕,因为验证很容易绕过。你的链接也坏了,但我设法找到了你在网站上提到的代码。太糟糕了,它受版权保护,我必须联系网站才能使用它。但它似乎仍然有效。
        • @Vilx- 如果文件路径有很长的随机字符串也没关系,它会定期更改并删除旧的。
        【解决方案5】:

        也许您可以在 lighttpd 中使用 mod trigger before download 或在 lighttpd 和 Apache2 中都使用 mod X-Sendfile 而不是在 Web 服务器中实现 Web 服务器?

        【讨论】:

        • 这样的模块会更有用:如果访问的 URL 被标记,则对特定的硬编码 URL 进行 HTTP 调用,转发所有接收到的标头。如果响应为 200,则允许访问,如果不是,则拒绝。 (我找不到这样的模块。)
        猜你喜欢
        • 2019-09-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-04
        • 2010-09-20
        • 2013-03-31
        • 2012-09-25
        • 2013-11-11
        • 2011-05-02
        相关资源
        最近更新 更多