doubai9014 2010-12-09 19:08
浏览 182
已采纳

通过PHP从外部Web服务流式传输大型文件

I am currently using external SOAP web services that allow chunked downloads/uploads of binary files (should allow larger files). I need to allow a end user to download files through the browser with a my PHP app. Serving small files works well, but 25MB+ files cause the web server to run out of memory.

I am using the native PHP Soap Client (no MTOM support), and requesting the download by submitting a form. Currently it seems like the web server is trying to download the whole file before outputing anything to the browser (e.g. the "Download" prompt doesn't show until after the whole file has been process via PHP).

My method looks something like this (sorry if it's messy, I've been hacking away at this problem for a while).

public function download()
{
    $file_info_from_ws ... //Assume setup from $_REQUEST params

    //Don't know if these are needed
    gc_enable();
    set_time_limit(0);
    @apache_setenv('no-gzip', 1);
    @ini_set('zlib.output_compression', 0);

    //File Info
    $filesize = $file_info_from_ws->get_filesize();
    $fileid = $file_info_from_ws->get_id();
    $filename = $file_info_from_ws->get_name();
    $offset = 0;
    $chunksize = (1024 * 1024);

    //Clear any previous data
    ob_clean();
    ob_start();

    //Output headers
    header('Content-Type: application/octet-stream');
    header('Content-Length: ' . $filesize);
    header('Content-Transfer-Encoding: binary');
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    header('Accept-Ranges: bytes');

    while($offset < $filesize)
    {
      $chunk = $this->dl_service()->download_chunked_file($fileid, $offset, $chunksize);
      if($chunk)
      {
        //Immediately echo out the stream
        $chunk->render();
        $offset += $chunksize;
        unset($chunk); //Shouldn't this trigger GC?
        ob_flush();
      }
    }
    ob_end_flush();
}

So my main question is: What is the best way to output large binary chunks from external resources (Webservices, DB, etc.) through PHP to the end user? Preferably without killing memory/CPU too much.

Also I'm curious about the following:
Why wouldn't the Download prompt pop-up after the first output?
Why is memory not freed after each loop in the about method?

  • 写回答

2条回答 默认 最新

  • doufei1988 2010-12-10 16:48
    关注

    Well I feel silly. This turned out to be just another PHP-ism. Apparently even though I was flushing the output buffer with ob_flush which (I thought) should have been sending the headings and chunks to the browser, the headers and output weren't actually getting flushed to the browser until the script finished.

    Even though the output is self was getting flushed, you still have to explicitly flush the write buffers of PHP and the web server back to the client. Not doing this lead to the memory expansion, and the download prompt not showing until the entire download completed.

    Here is a version of the working method:

    public function download()
    {
        $file_info ... //Assume init'ed from WS or DB
    
        //Allow for long running process
        set_time_limit(0);
    
        //File Info
        $filesize = $file_info->get_filesize();
        $fileid = $file_info->get_id();
        $filename = $file_info->get_name();
        $offset = 0;
        $chunksize = (1024 * 1024);
    
        //Clear any previous data
        ob_clean();
        ob_start();
    
        //Output headers to notify browser it's a download
        header('Content-Type: application/octet-stream');
        header('Content-Length: ' . $filesize);
        header('Content-Disposition: attachment; filename="' . $filename . '"');
    
        while($offset < $filesize)
        {
          //Retrieve chunk from service
          $chunk = $this->dl_service()->download_chunked_file($fileid, $offset, $chunksize);
          if($chunk)
          {
            //Immediately echo out the stream
            $chunk->render();
            //NOTE: The order of flushing IS IMPORTANT
            //Flush the data to the output buffer
            ob_flush(); 
            //Flush the write buffer directly to the browser
            flush();
            //Cleanup and prepare next request
            $offset += $chunksize;
            unset($chunk);
          }
        }
        //Exit the script immediately to prevent other output from corrupting the file
        exit(0);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能