2013-10-18 118 views
1

我試圖在Kohana 3.3上使用ZipStream(https://github.com/Grandt/PHPZip)創建和流式傳輸zip文件。我認爲一旦將第一張圖像添加到壓縮文件中,壓縮文件就會流式傳輸,但是直到整個壓縮文件被創建併發送給用戶時,下載纔會停止。ZipStream和Kohana 3.3

<?php defined('SYSPATH') or die('No direct script access.'); 

class Controller_Download extends Controller { 
    public function action_images() 
    { 
     require Kohana::find_file('vendor', 'ZipStream'); 
     $zip = new ZipStream("images.zip"); 
     foreach($images as $image) 
     { 
      $zip->addLargeFile($image); 
     } 
     $zip->finalize(); 
     exit; 
    } 
} 

回答

1

顯然Kohana緩衝區輸出,可以通過添加此操作來取消下載操作。

while (ob_get_level() > 0) { 
    ob_end_clean(); 
} 

所有控制器

<?php defined('SYSPATH') or die('No direct script access.'); 

    class Controller_Download extends Controller { 
     public function action_images() 
     { 
      while (ob_get_level() > 0) { 
       ob_end_clean(); 
      } 
      require Kohana::find_file('vendor', 'ZipStream'); 
      $zip = new ZipStream("images.zip"); 
      foreach($images as $image) 
      { 
       $zip->addLargeFile($image); 
      } 
      $zip->finalize(); 
      exit; 
     } 
    }