create and download zip file using php
Why not use the Zip Encoding Class in Codeigniter - it will do this for you
$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');
... it work for me
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'); }
What is
asset_url()
function? Try to useAPPPATH
constant istead this function:$resumePath = APPPATH."../uploads/resume/";
Add "exists" validation for file names:
foreach ($fileNames as $files) { if (is_file($resumePath . $files)) { $zip->addFile($resumePath . $files, $files); }}
Add
exit()
after:echo json_encode("Cannot Open");
Also I think it's the better desision to use CI zip library User Guide. Simple example:
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);}