Create and Download Zip file in CodeIgniter

In PHP, ZipArchive Class is used for creating a zip file.

For adding a whole directory files need to read directory files one by one and add in the ZipArchive Class object.

CodeIgniter already provided a zip library that allows either add files or a directory for Compress.

Need to load the zip library for using it.

In this tutorial, I show how you can create a zip file, download, and save it to the server with CodeIgniter.

Create and Download Zip file in CodeIgniter


Contents

  1. Store files
  2. Configuration
  3. Controller
  4. View
  5. Demo
  6. Conclusion

1. Store files

  • Created a uploads directory at the project root.

Create and Download Zip file in CodeIgniter

  • Store files in uploads directory and created another sub-directory documents for storing files.

Create and Download Zip file in CodeIgniter

  • I am using uploads directory in zip file creation.
  • Create archivefiles directory at project root to save the created zip files.

2. Configuration

Default controller

Open application/config/routes.php and edit default_controller value to Zip.

$route['default_controller'] = 'Zip';

3. Controller

Create a Zip.php file in application/controllers/ directory.

Create 3 methods –

  • __construct – Load url helper and zip library.
  • index – Load index_view view.
  • createzip – This method is called on <form > submit.

Add files –

On the first button click, I am adding specified files for the compress.

For this, I have assign file path in $filepath1 and $filepath2.

To add file use $this->zip->read_file();. This method takes file-path as a parameter.

$this->zip->read_file([file-path]);

Pass the $filepath1 and $filepath2 in the method.

For downloading file on the user system call $this->zip->download() method. This method takes file-name as a parameter.

$this->zip->download([file-name]);

Add directory files and sub-directory –

On the second button click, I am adding whole directory files and sub-directory for compress.

Specify a directory path in the $path.

Use $this->zip->read_dir() to add directory files.

$this->zip->read_dir([directory-path]);

To save a zip file on a server call $this->zip->archive() method and pass the path with file-name where you want to store.

$this->zip->archive([file-path]);

Execute $this->zip->download() method for downloading the file to the user system.

Completed Code

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Zip extends CI_Controller {

  public function __construct(){

     parent::__construct();
     $this->load->helper('url');

     // Load zip library
     $this->load->library('zip');

  }

  public function index(){
     // Load view
     $this->load->view('index_view');
  }

  // Create zip
  public function createzip(){

     // Read file from path
     if($this->input->post('but_createzip1') != NULL){

        // File path
        $filepath1 = FCPATH.'/uploads/image1.jpg';
        $filepath2 = FCPATH.'/uploads/document/users.csv';

        // Add file
        $this->zip->read_file($filepath1);
        $this->zip->read_file($filepath2);

        // Download
        $filename = "backup.zip";
        $this->zip->download($filename);

     }

     // Read files from directory
     if($this->input->post('but_createzip2') != NULL){
        // File name
        $filename = "backup.zip";
        // Directory path (uploads directory stored in project root)
        $path = 'uploads';

        // Add directory to zip
        $this->zip->read_dir($path);

        // Save the zip file to archivefiles directory
        $this->zip->archive(FCPATH.'/archivefiles/'.$filename);

        // Download
        $this->zip->download($filename);
     }

     // Load view
     $this->load->view('index_view');
  }

}

4. View

Create a index_view.php file application/views/ directory.

Create a <form > and set action='<?= base_url() ?>index.php/zip/createzip‘.

Create two submit buttons.

  • One for creating a zip file from the specified path.
  • and another for creating zip file from a directory.

Completed Code

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Create and Download Zip file in CodeIgniter</title>
  </head>
  <body>

    <form method='post' action='<?= base_url() ?>index.php/posts/createzip/'>
       <input type="submit" name="but_createzip1" value='Add file from path and download zip'>
       <input type="submit" name="but_createzip2" value='Add directory files and sub-directory, save archive and download zip'>
    </form>

  </body>
</html>

5. Demo

Click on a button.


6. Conclusion

If you want to add specific files then use read_file() method or use read_dir() method to add whole files and sub-directory for compress.

To save the created zip file use archive() method.

You can learn more about the zip library from here.

If you found this tutorial helpful then don't forget to share.

Leave a Comment