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(controllers and views).
First Create a View file to upload files, upload_form_view.php in views {applications/views/upload_form_view.php}
Upload Form
Then Create a view File to show Success Message and uploaded files , upload_success_view.php in views { applications/views/upload_success_view.php }
Upload Form Your file was successfully uploaded!
-
$value):?>
- :
Then Create A Controller Called upload.php in Controllers( applications/controller/upload.php).
class Upload extends CI_Controller { function __construct() { parent::__construct(); // load ci's Form and Url Helpers $this->load->helper(array('form', 'url')); } function index() { $this->load->view('upload_form_view', array('error' => ' ' )); } function file_upload() { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'zip'; $config['max_size'] = ''; $this->load->library('upload', $config); if ( ! $this->upload->do_upload()) { $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_form_view', $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_view', $data); } } }
Notes :
1. PHP ZipArchieve extension can be used to extract zip files. Allow this extension in php.ini if not enabled by default.
2. Create A Folder uploads in application Root Directory
.