create and download zip file using php

15,632

Solution 1

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

Solution 2

  1. What is asset_url() function? Try to use APPPATH constant istead this function:

    $resumePath = APPPATH."../uploads/resume/";
  2. Add "exists" validation for file names:

    foreach ($fileNames as $files) {
        if (is_file($resumePath . $files)) {
            $zip->addFile($resumePath . $files, $files);
        }
    }
  3. 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);
}
Share:
15,632
Mohammed Sufian
Author by

Mohammed Sufian

A Hack Programmer, capable writing codes using almost all programming languages. He is also involved in maintaining & securing Linux/Windows Servers (Vulnerability / Penetration Tester) Of course, I can't survive without the Internet & I guess you too ;-) Quick Learning & Implementing Ability... Googling and implementing, optimizing, testing, and using best practices... You can connect with Sufian using mohammedsufianshaikh at gmail

Updated on June 29, 2022

Comments

  • Mohammed Sufian
    Mohammed Sufian almost 2 years

    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