Upload Zip file and Extracting this Zip file using Codeigniter

Codeigniter

Hi everyone,

In this tutorial explains how to upload a Zip file and extract in codeigniter. Upload the file using codeigniters file upload class and then extracting the uploaded file. Codeigniter’s file uploading class permits users to set some preferences for uploading files like file type, size of file, there for we can set the allowed type as zip.

Please follow steps to upload a zip file and extracting the zip file.

Process

Step -1 : Create a form to upload  file.

  • add enctype as multipart/form-data
  • input field type is file for uploading files

save the file aszipandunzip_v.php in your view folder of codeigniter application.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<html> 
<head> 
<title>Zip File Upload and Extract It</title> 
<!-- Latest compiled and minified CSS --> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/
bootstrap/3.3.7/css/bootstrap.min.css">
 <!-- jQuery library --> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/
jquery.min.js"></script>
 <!-- Latest compiled JavaScript --> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/
bootstrap.min.js">
</script> 
<style type="text/css"> 
.container{ 
margin-top: 50px;
 border:1px solid rgba(158, 158, 158, 0.49); 
padding-bottom: 50px; 
}
 h2{ text-align: center; } 
h4{ margin-top:20px; color:gray; 
} 
</style> 
</head> 
<body> 
<div class="container"> 
<h2>Zip File Upload and Extracting this file In Codeigniter</h2>
 <form method='POST' enctype="multipart/form-data">  
<div class="form-group"> 
<div class="col-md-10">
<input type="file" name="fileUpload" class="form-control">
</div> </div> 
<div class="form-group"> 
<div class="col-md-2">
<input type="submit" name="fileUploadSubmit" value="UPLOAD & UNZIP"
 class="form-control btn btn-info">
</div> </div> 
</form> 
<div style="margin:10px;font-weight:bolder;"><i> 
<?php     
if(isset($_SESSION['msg'])) echo $_SESSION['msg']; 
if(isset($_SESSION['error'])) echo $_SESSION['error']; 
?> </i></div> 
</div> 
</body>
</html>

step -2 : For uploading and extracting a file, create a new folder in your root directory and name it as “uploads” and mention this name in uploaded path.

step -3 : Now we need to upload the selected files into uploads folder of your codeigniter application. For this, create a new controller called “ZipandUnzip.php” in your controllers folder of codeigniter application.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class ZipandUnzip extends CI_Controller {

        function __construct() { 
               parent::__construct(); 
               $this->load->library(['session']); 
               $this->load->helper(['url','file','form']); 
        }
        public function index()
	{
		$this->load->view('zipandunzip_v');
	}
	public function fileupload()
	{
		if(isset($_POST['fileUploadSubmit']))
		{ 
			if($_FILES['fileUpload']['name'] !='')
			{
				$config['upload_path'] = './uploads/'; 
                                $config['allowed_types'] = 'zip';
                                $config['encrypt_name'] = TRUE; 
                                $this->load->library('upload');
             	                $this->upload->initialize($config);
	                        if (!$this->upload->do_upload('fileUpload'))
		                {
		                        $this->session->set_flashdata('error',$this->
upload->display_errors());
		                        redirect('zipandUnzip/index');
		                }
		                else
		                {
		                        $data      = $this->upload->data();
		                        $zip_file  = new ZipArchive;
 					$full_path = $data['full_path'];
				        if ($zip_file->open($full_path) === TRUE) 
					{
					       $zip_file->extractTo(base_url().'/uploads/
');
					       $zip_file->close();
					       $this->session->set_flashdata('msg',
'Uploaded the Zip file and the Extracted Successfully');
	                                }else
				       {
					       $this->session->set_flashdata('error',
'Failed to extract');
				       }
								
				     redirect('zipandUnzip/index');
				}
			}else
			{
				$this->session->set_flashdata('error','File not 
selected');
				redirect('zipandUnzip/index');
			}
		}
		redirect('zipandUnzip/index');
	}
}
?>

Step -4 : After that,execute the code and check the uploads folder under root directory to view the uploaded file and unzip file.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *