Create and upload zip file using php

I am trying to create a zip file (using php), for this I wrote 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, when I press the button, I get nothing .. not even any errors or messages.

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

+3
source share
5 answers

Why not use the Zip Encoding Class in Codeigniter - it will do it 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');

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

+6
source
  • What is a function asset_url()? Try using the APPPATHistead constant for this function:

    $ resumePath = APPPATH. "../ uploads / resume /";
  • Add a confirmation of "exists" for the file names:

    foreach ($fileNames as $files) {
        if (is_file($resumePath . $files)) {
            $zip->addFile($resumePath . $files, $files);
        }
    }
  • exit() :

    echo json_encode("Cannot Open");

, CI zip . :

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);
}
+1

...

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

. 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();
0

/* zip */

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();
}
0

All Articles