Uploading and Extracting Zip Files with Codeigniter and PHP Ziparchive
Codeigniter file upload library can be used in web applications which requires user to upload files to the web server. Codeigniter provides complete list of functionalities regarding file uploading. Sometimes we may need to upload multiple files to the server. So if the application provides a single file upload at a time this becomes a very hard task. As a solution to that problem we can create a multiple file upload by using zip files. We can create a zip file including all the files we want to upload and then upload it using single file upload component. Then the application will automatically unzip and extract the files inside the zip folder which saves time.
In this post i’ll explain how to upload and extract zip files using codeigniter file upload library and PHP Zipachive library. Lets start by creating required codeigniter files.
View file for file upload – upload_file.php
Upload Form
View file foe upload success – upload_success.php
Upload Form Your file was successfully uploaded!
-
$value):?>
- :
Controller Class for file upload – upload.php
class Upload extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper(array('form', 'url')); } function index() { $this->load->view('upload_file', array('error' => ' ' )); } function file_upload() { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'zip'; $config['max_size'] = '100'; $this->load->library('upload', $config); if ( ! $this->upload->do_upload()) { $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_form', $error); } else { $data = array('upload_data' => $this->upload->data()); $zip = new ZipArchive; $file = $data['upload_data']['full_path']; chmod($file,0777); if ($zip->open($file) === TRUE) { $zip->extractTo('./uploads/'); $zip->close(); echo 'ok'; } else { echo 'failed'; } $this->load->view('upload_success', $data); } } }
Code Explanation
- You can load the upload file view by using http://www.example.com/upload.
- index function of Upload class is called and it will load the upload_file view file.
- Select a zip file and click upload button to upload the zip file to the server.
- Now the file_upload method will be called and we can set the configurations for upload at the top of the function.
- Here i have set the upload directory, allowed file types and max size of the file.
- Since we are only using zip files i have restricted the uploading to only zip files.
- Then upload library is loaded and configuration settings are passed as an array.
- Then we call the do_upload function to upload the file to the server.
- It will automatically upload the file according to the given config and will display an error on failure or goes to sucess page on success.
- If the upload is suuccessfull we set the permission to the uploaded file in order to extract it.
- PHP ZipArchieve extension can be used to extract zip files. Allow this extension in php.ini if not enabled by default.
- Next we check if the zip file cannot be opened properly using open method in the ZipArchieve class
- Then we can provide the location to extract the zip file and it will be extracted automatically.
Leave a Reply