Thursday, February 6, 2020

How To Create And Download Zip File In Codeigniter

How To Create And Download  Zip File In Codeigniter

In this post we will discuss how we can generate a ZIP file with multiple files after saving this folder and finally download the generated ZIP file using the Codeigniter framework. Codeigniter is one of the best PHP frameworks that speeds up your web development process. Many web developers have used this framework to develop their web applications because it is a very popular PHP framework. Many utilities and built-in libraries have been provided to help you develop your web application very quickly. With this framework you can perform many tasks very easily. So here we also have a task on how to create a zip file under Codeigniter.


Codeigniter Zip Encoding Class


If you've used the Codeigniter framework, it's very easy to create a zip file. Because Codeigniter has its own integrated zip library. This class is used to generate zip files. Zip files can also be downloaded to our local computer. Now how to initialize this zip class. To do this, we need to write the following code.


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


With the code above, you can initialize the Zipign code classiter. Then you can use another method of this class by calling the object $ this-> zip. Now we want to create a zip file of selected images from the image list. For this we use the following method of this zip library.

read_file (): This method reads the contents of a file and adds it to Zip.

add_data (): This method adds data to the zip file. It defines the file path and the data of this file.

archive (): This method writes the file to the specified directory. With this method, we need to define the name of the file with the full path.

download () - This method downloads the zip file created on our local computer. With this method we have to define the name of the zip file.

The Codeigniter Zip class method in particular helps us to generate a zip file, save it in a folder and download it to our local computer. Below is the full source code of this tutorial.


Source Code


Zip_file.php(Controllers)



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

class Zip_file extends CI_Controller {
 
 function index()
 {
  $directory = 'download';
  $data["images"] = glob($directory . "/*.jpg");
  $this->load->view('zip_file', $data);
 }

 function download()
 {
  if($this->input->post('images'))
  {
   $this->load->library('zip');
   $images = $this->input->post('images');
   foreach($images as $image)
   {
    $this->zip->read_file($image);
   }
   $this->zip->download(''.time().'.zip');
  }
 }

}

?>


zip_file.php



<html>
<head>
    <title>How To Create And Download  Zip File In Codeigniter</title>
    
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    
</head>
<body>
 <div class="container box">
  <h3 align="center">How To Create And Download  Zip File In Codeigniter</h3>
  <br />
  <form method="post" action="<?php echo base_url(); ?>zip_file/download">
  <?php
  foreach($images as $image)
  {
   echo '
   <div class="col-md-2" align="center" style="margin-bottom:24px;">
    <img src="'.base_url().''.$image.'" class="img-thumbnail img-responsive" />
     <br />
    <input type="checkbox" name="images[]" class="select" value="'.$image.'" />
   </div>
   ';
  }
  ?>
  <br />
  <div align="center">
   <input type="submit" name="download" class="btn btn-primary" value="Download" />
  </div>
  </form>
 </div>
</body>
</html>

<script>
$(document).ready(function(){
 $('.select').click(function(){
  if(this.checked)
  {
   $(this).parent().css('border', '5px solid #ff0000');
  }
  else
  {
   $(this).parent().css('border', 'none');
  }
 });
});
</script>


0 comments:

Post a Comment

Please don't enter any spam link in the comment box.