Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create and download zip file using php

i am trying to create a zip file(using php) for this i have written the following code:

$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;

however on a button click i am not getting anything..not even any error or message..

any help or suggestion would be a great help for me.. thanks in advance

like image 466
Mohammed Sufian Avatar asked Feb 20 '26 05:02

Mohammed Sufian


2 Answers

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');

https://www.codeigniter.com/user_guide/libraries/zip.html

like image 145
Zabs Avatar answered Feb 23 '26 01:02

Zabs


... 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');
    }

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!