Rabu, 06 Maret 2019

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 File 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:

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

Once loaded, the Zip library object will be available using:

$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.

/**
* 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('
', '
');
$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

load->view('templates/header'); ?>


Create and Download Zip File using Codeigniter








$element) { ?>














load->view('templates/footer'); ?>


Demo  [sociallocker] Download[/sociallocker]
Disqus Comments