создавать и загружать zip-файл, используя php

Я пытаюсь создать zip-файл (используя php), для этого я написал следующий код:

$fileName = "1.docx,2.docx"; $fileNames = explode(',', $fileName); $zipName = 'download_resume.zip'; $resumePath = asset_url() . "uploads/resume/"; //http://localhost/mywebsite/public/uploads/resume/ $zip = new ZipArchive(); if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== TRUE) { echo json_encode("Cannot Open"); } foreach ($fileNames as $files) { $zip->addFile($resumePath . $files, $files); } $zip->close(); header("Content-type: application/zip"); header("Content-Disposition: attachment; filename=".$zipName.""); header("Content-length: " . filesize($zipName)); header("Pragma: no-cache"); header("Expires: 0"); readfile($zipName); exit; 

однако при нажатии кнопки я ничего не получаю .. даже никаких ошибок или сообщений.

любая помощь или предложение были бы большой помощью для меня .. спасибо заранее

Почему бы не использовать Zip Encoding Class в Codeigniter – он сделает это за вас

 $name = 'mydata1.txt'; $data = 'A Data String!'; $this->zip->add_data($name, $data); // Write the zip file to a folder on your server. Name it "my_backup.zip" $this->zip->archive('/path/to/directory/my_backup.zip'); // Download the file to your desktop. Name it "my_backup.zip" $this->zip->download('my_backup.zip'); 

http://ellislab.com/codeigniter%20/user-guide/libraries/zip.html

  1. Что такое asset_url() ? Попробуйте использовать константу APPPATH istead для этой функции:

      $ resumePath = APPPATH. "../ uploads / resume /"; 
  2. Добавьте подтверждение «существует» для имен файлов:

      foreach ($ fileNames как $ files) {
         if (is_file ($ resumePath. $ files)) {
             $ zip-> addFile ($ resumePath. $ files, $ files);
         }
     } 
  3. Добавьте exit() после:

      echo json_encode («Не удается открыть»); 

Также я считаю, что лучше использовать CI zip-библиотеку User Guide . Простой пример:

 public function generate_zip($files = array(), $path) { if (empty($files)) { throw new Exception('Archive should\'t be empty'); } $this->load->library('zip'); foreach ($files as $file) { $this->zip->read_file($file); } $this->zip->archive($path); } public function download_zip($path) { if (!file_exists($path)) { throw new Exception('Archive doesn\'t exists'); } $this->load->library('zip'); $this->zip->download($path); } 

… это работает для меня

 public function downloadall(){ $createdzipname = 'myzipfilename'; $this->load->library('zip'); $this->load->helper('download'); $cours_id = $this->input->post('todownloadall'); $files = $this->model_travaux->getByID($cours_id); // create new folder $this->zip->add_dir('zipfolder'); foreach ($files as $file) { $paths = 'http://localhost/uploads/'.$file->file_name.'.docx'; // add data own data into the folder created $this->zip->add_data('zipfolder/'.$paths,file_get_contents($paths)); } $this->zip->download($createdzipname.'.zip'); } - public function downloadall(){ $createdzipname = 'myzipfilename'; $this->load->library('zip'); $this->load->helper('download'); $cours_id = $this->input->post('todownloadall'); $files = $this->model_travaux->getByID($cours_id); // create new folder $this->zip->add_dir('zipfolder'); foreach ($files as $file) { $paths = 'http://localhost/uploads/'.$file->file_name.'.docx'; // add data own data into the folder created $this->zip->add_data('zipfolder/'.$paths,file_get_contents($paths)); } $this->zip->download($createdzipname.'.zip'); } 

Ниже сценариев работает нормально в моей локальной системе. 1-й удалить asset_url () из $ resumePath и установить относительный путь местоположения хранилища zip-файла. – Пропустить zip-файл с его маршрутом местоположения до $ zip-> open ()

 $fileName = "1.docx,2.docx"; $fileNames = explode(',', $fileName); $zipName = 'download_resume.zip'; $resumePath = "resume/"; $zip = new ZipArchive(); if ($zip->open($resumePath.$zipName, ZIPARCHIVE::CREATE) !== TRUE) { echo json_encode("Cannot Open"); } foreach ($fileNames as $files) { $zip->addFile($files, $files); } $zip->close(); 

/ * создать почтовую папку * /

 public function zip(){ $getImage = $this->cart_model->getImage(); $zip = new ZipArchive; $auto = rand(); $file = date("dmYhis",strtotime("Y:m:d H:i:s")).$auto.'.zip'; if ($zip->open('./download/'.$file, ZipArchive::CREATE)) { foreach($getImage as $getImages){ $zip->addFile('./assets/upload/photos/'.$getImages->image, $getImages->image); } $zip->close(); $downloadFile = $file; $download = Header("Location:http://localhost/projectname/download/".$downloadFile); } } 

модель——

/ * добавить изображение в корзину * /

 public function getImage(){ $user_id = $this->session->userdata('user_id'); $this->db->select('tbl_cart.photo_id, tbl_album_image.image as image'); $this->db->from('tbl_cart'); $this->db->join('tbl_album_image', 'tbl_album_image.id = tbl_cart.photo_id', 'LEFT'); $this->db->where('user_id', $user_id); return $this->db->get()->result(); }