Codeigniter Zip Library Functions
Codeigniter has a built in library to manipulate zip file related activities.You can create and download zip files using this zip encoding class. In this post i’am going to explain how to select multiple files from server and download it as a zip file using this library.
Loading the Zip Encoding Class
$this->load->library('zip');
Add files to Zip Archive with read_file function
You can add a file in your server to zip file using read_file function. Define the path to the file and pass it to read_file function as a parameter and the file will be added into currently loaded zip object. If you want to add multiple files to zip archive repeat the following line of code with different paths.
$path = '/path/file_name'; $this->zip->read_file($path);
Download Zip Archive with download function
You can download the currently used zip object using the download method of the zip library. Pass the file name as the paramter and zip file will be downloaded automatically. Currently loaded files using the read_file method will be included inside the zip archive.
$this->zip->download('sample_zip.zip');
How to Browse and Download Multiple Files Instantly
First we have to create a controller function and view file to display the files inside a folder in your server. Normally we want to download the files inside the upload folder.
Controller Function to Display Files
function index() { $file_path = './uploads/'; $files = scandir($file_path); $files_array = array(); foreach($files as $key=>$file_name) { $file_name = trim($file_name); if($file_name != '.' || $file_name != '..') { if((is_file($file_path.$file_name))) { array_push($files_array,$file_name); } } } $data['files'] = $files_array; $this->load->view('view_files', $data); }
Code Explanation
- First we get the files inside the given file path using the scandir function.
- Then we can traverse through each file and checks if the name is actually a file or directory.
- If it is a file we pass the file name to the files array
- Then we can assign the files array to the view file to display.
View File to Display Files
Code Explanation
- Inside the view file we create a form with text field for file name and submit button.
- Then we can traverse through each file and display inside a html table with checkbox option.
- Inside the script code we can validate the file name and files to download using jquery.
- Then we can submit the form on successfull validation.
Controller Function to Download Files
function download_zip() { $this->load->library('zip'); $file_path = './uploads/'; $zip_file_name = $_POST['file_name']; $selected_files = $_POST['files']; foreach($selected_files as $key=>$file){ $this->zip->read_file($file_path.$file); } $this->zip->download($zip_file_name); }
Code Explanation
- Once the form is submitted download_zip function is called.
- We can get the file name for the zip and selected files to download using $_POST array.
- Then we traverse through each selected file and add it to zip file using read_file function.
- Once we call the download function on the current zip object pop up will open with download option.
Download Source Codes for Download Multiple Files using Zip
Download Code
Leave a Reply