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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line