How to zip, save and download a file in codeigniter

How to zip, save, download a file in codeigniter

In this article we are going to see how to zip, save, download a file in codeigniter PHP framework. codeigniter is a very popular PHP framework. Previous article is about how to upload  a file using codeigniter PHP framework. Codeigniter framework you can do many tasks very easily. Compressing and downloading or saving can be done very easily.

zip, save, download a file in codeigniter

Zip Encoding Class

Zip class is used to create and download archives.  In controller you can initialize the zip class using

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

After loading the library we can use the zip object.

$this->zip

 Zip, Save and Download a file

The code snippet below shows how to create a zip file, save it and download it.

$filename = 'example.txt';

$filedata = 'This is an example file. Codeigniter zip class is used to zip, save and download the file';

$this->zip->add_data($filename, $filedata);

//Write the zip file to a folder on server with name. "example_backup.zip"

$this->zip->archive('/images/example_backup.zip');

//Download the file to system. Name it "my_backup.zip"
$this->zip->download('example_backup.zip');

Adding Controller in CodeIgniter

So if you want to zip, save, download a file in codeigniter then you have to do following

1. In Controller folder, Add a controller file and name it as, “Zip.php”

2. Add a folder named “zippedfiles” in root folder.

3. Add the code below to the Zip.php file. 4. Run the code, a zip file is created and downloaded to your computer

Codeigniter controller code

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

  class Zip extends CI_Controller {

        public function __construct()
        {
            parent::__construct();
            $this->load->library('zip');
        }

        public function index()
        {
        
            $filename = 'example.txt';
            $filedata = 'This is an example file. Codeigniter zip class is 
                         used to zip, save and download the file';

            $this->zip->add_data($filename, $filedata); 
            
            //Create zip file on server with name."example_backup.zip"
            $this->zip->archive('/zippedfiles/example_backup.zip');  

            //Download the file to your system. It will be named "example_backup.zip"
            $this->zip->download('example_backup.zip');
        }
    }

Code Explanation

In code above We have created a class named Zip that extends CI_Controller. Inside constructor, that runs each time when a class object is created.  After calling parent constructor, we load zip library.

Inside index action we create a variable name $filename and a $filedata with content.

add_data method

$this->zip->add_data($filename, $filedata);

Zip object’s add_data method is called with $filename and $filedata params.

archive method

$this->zip->archive('/zippedfiles/example_backup.zip');  

The code above, archive method of zip object saves file to a specified path.

download method

$this->zip->download('example_backup.zip');

Finally the download method of zip class object downloads file to user’s computer.  The code above can be used in any of your project to zip and download zip archives.

Please leave your valuable feedback and comments below.

About Jason William