Create and Download Zip File using CodeIgniter
A ZIP file is an archive that contains one or more files or directories compressed. Zip files make it easy to keep related files or directories together and make transporting, e-mailing, downloading, and storing data and software faster and more efficient. In this tutorial, We have share how to Create and Download Zip Files using CodeIgniter zip library. This example demonstrates how to compress a file, save it to a folder on your server, and download your computer.
Step 1: load class
The Zip class is initialised in your controller using the
$this->load->library
function:
1 2 3 |
<?php $this->load->library('zip'); ?> |
Once loaded, the Zip library object will be available using:
1 2 3 |
<?php $this->zip ?> |
read_file()
– This method will read the content of a file and it will add into Zip.archive()
– This method will write file to the specified directory, in this method we have to define file name with complete path.download()
– This method will download created Zip file in our local, in this method we have to define zip file name.
Step 2: Create a controller file
Next create a controller file named “
Zipfile.php
” inside “application/controllers” folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<?php /** * Description of Zipfile Controller: CodeIgniter * * @author TechArise Team * * @email info@techarise.com */ if (!defined('BASEPATH')) exit('No direct script access allowed'); class Zipfile extends CI_Controller { public function __construct() { parent::__construct(); // load form validation library $this->load->library('form_validation'); // load zip library $this->load->library('zip'); } // get file list public function index() { $data = array(); $data['page'] = 'create-download-zip'; $data['title'] = 'Create and Download Zip | TechArise'; $data['breadcrumbs'] = array('Home' => '#'); $directory = ROOT_UPL_IMAGE_PATH; $files = glob($directory .'*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE); $data['fileInfo'] = $files; $this->load->view('zipfile/index', $data); } // create and download method public function create() { // form validation $this->form_validation->set_error_delimiters('<div class="alert alert-danger">', '</div>'); $this->form_validation->set_rules('file_name[]', 'file', 'required', array('required' => 'Multiple checkboxes at least 1 required.')); if ($this->form_validation->run() == FALSE) { $this->index(); } else { $fileName = 'my_backup.zip'; if(!empty($this->input->post('file_name')) && count($this->input->post('file_name'))>0) { $files = $this->input->post('file_name'); foreach($files as $file) { $this->zip->read_file(ROOT_UPL_IMAGE_PATH.$file); } // Write the zip file to a folder on your server. Name it "my_backup.zip" $this->zip->archive(ROOT_UPL_IMAGE_PATH.$fileName); // Download the file to your desktop. Name it "my_backup.zip" $this->zip->download($fileName); } } } } ?> |
Step 3: Create a view file
Create a view file named “
index.php
” inside “application/views/zipfile folder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php $this->load->view('templates/header'); ?> <div class="container"> <div class="row"> <div class="col-sm-12 col-md-12"><h3>Create and Download Zip File using Codeigniter</h3></div> <div class="col-sm-12 col-md-12"><?php echo validation_errors(); ?></div> <form action="<?php print site_url()?>zipfile/create" class="spsec-validation" id="spsec-validation" enctype="multipart/form-data" method="post" accept-charset="utf-8" novalidate="novalidate"> <div class="col-sm-12 col-md-12"> <table class="table table-bordered table-hover table-striped print-table order-table"> <?php foreach (array_chunk($fileInfo, 3, true) as $array_chunk) { ?> <tr> <?php foreach ($array_chunk as $key => $element) { ?> <td> <input type="checkbox" name="file_name[]" value="<?php print basename($element); ?>"> <?php print basename($element); ?><br> <img src="<?php print HTTP_UPL_IMAGE_PATH.basename($element);?>" style="width:180px; height:180px;"> </td> <?php } ?> </tr> <?php } ?> </table> </div> <div class="col-sm-12 col-md-12"> <div class="form-group text-right"> <button type="submit" name="create_zip" id="create-zip" class="btn btn-success"> <i class="fa fa-download" aria-hidden="true"></i> Download</button> </div> </div> </form> </div> </div> <?php $this->load->view('templates/footer'); ?> |