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. A 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 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 to zip, save, download a file
So if you want to zip, save, download a file in codeigniter then you have to do following
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
<?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'); } }
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.
Related Articles:
- How to watermark images using codeigniter php framework
- How to zip or unzip files using NodeJS and Express JS framework
- How to create RESTful web services in codeigniter
- Nodejs mongodb tutorial – find, insert, update, delete records
Previous Article:
Next Article: