2014-10-08 37 views
-2

我正在開發一個基於CodeIgniter的web應用程序,並且我正在實現下載管理中的車輛圖像的zip文件的功能,以傳遞給第三方派對。可以在PHP中創建zip存檔,但無法正確下載

我可以毫無問題地創建zip存檔 - 如果我註釋掉後面的部分,我可以看到zip文件被創建幷包含正確的文件,並且可以正常提取。

但是,允許用戶在創建文件後下載該文件是非常棘手的。我已經實現了允許與要下載的正確名稱的文件,但它的錯誤時,以下消息解壓:

End-of-central-directory signature not found. Either this file is not 
    a zipfile, or it constitutes one disk of a multi-part archive. In the 
    latter case the central directory and zipfile comment will be found on 
    the last disk(s) of this archive. 

任何想法,我已經錯了嗎?我在網上查了多篇教程,並找不到問題出在哪裏。

public function downloadvehicleimages() 
{ 
    // If logged in... 
    if ($this->adminuser) 
    { 
     // Get data 
     $usedcars = $this->adminmodel->getUsedCarsWithLimit(); 

     // Get the registrations for these vehicles 
     $registrations = array(); 
     foreach($usedcars->result_array() as $usedcar) 
     { 
      array_push($registrations, $usedcar['Registration']); 
     } 

     // Get all the images for these registrations 
     $images = array(); 
     $original_dir = getcwd(); 
     chdir('../used-cars/static/images/custom/'); 
     foreach($registrations as $registration) 
     { 
      $vehicle_images = glob(strtoupper($registration) . '_*.jpg'); 
      sort($vehicle_images); 
      foreach($vehicle_images as $vehicle_image) 
      { 
       array_push($images, $vehicle_image); 
      } 
     } 

     // Zip them up 
     $zipname = 'images.zip'; 
     $zip = new ZipArchive; 
     $zip->open($zipname, ZipArchive::CREATE); 
     foreach($images as $image) 
     { 
      $zip->addFile($image); 
     } 
     $zip->close(); 

     // Let user download the file 
     $this->output->set_header('Content-Type: application/zip'); 
     $this->output->set_header('Pragma: no-cache'); 
     $this->output->set_header('Expires: 0'); 
     $this->output->set_header("Content-Disposition: attachment;filename='$zipname'"); 
     $this->output->set_header('Content:Length: ' . filesize($zipname)); 
     unlink($zipname); 
     chdir($original_dir); 
    } 
} 
+0

我會接受的答案的人只是把,但它刪除了 – 2014-10-08 10:00:13

回答

2

您需要使用fopen()來讀取zipfile的內容,並使用readfile()將文件內容傳遞給訪問者。而且,提供完整的系統目錄的URI的文件位置:

 $filename = 'images.zip'; 
     $path = /var/www/yourdomain.com/images.zip'; 
     if(!file_exists($path)) 
     { 
      die('Error: file not found'); 
     } 
     else { 
      $handle = fopen($path, "r"); 
      header('Content-Description: File Transfer'); 
      header('Content-Type: application/zip'); 
      header('Content-Disposition: attachment; filename='.$filename); 
      header('Content-Transfer-Encoding: binary'); 
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
      header('Expires: 0'); 
      header('Pragma: public'); 
      header('Content-Length: ' . filesize($path)); 

      flush(); 
      readfile($path); 
      fclose($handle);   
+0

欣賞的努力,但別人跳樓在較短的解決方案,它的工作原理(在CodeIgniter中使用force_download()輔助方法),但由於某種原因,他們刪除了它。 – 2014-10-08 10:11:20

+0

對於那些不使用CodeIgniter(與我一樣)的fopen/flush/readfile/fclose解決方案就像一個魅力:) – dsnunez 2014-12-09 14:24:41