Tuesday, 3 May 2016

Upload and Extract a Zip File in Php


If you are looking to learn php advance concept like upload zip file and extract that file on server using php script. So in this post I will show you how to extract zip file in php and after from that uploaded file i will extract zip in upload folder after this I will copy all file from extracted folder to upload folder and remove zip file from upload folder and lastly remove extracted folder from upload file folder. I have show you one simple application, in which I want to upload images in very large amount for this I want to upload images in zip folder. From Zip folder I will extract zip folder and get images in to my upload directory and remove zip file and extracted folder. For this things I have use Zip Archive class and For this zip archive class first you have to enable php zip extension from php the list of php extension. I have create one zip object from Zip Archive class. You can find details description under video of this post.

Source Code

 <?php  
 if(isset($_POST["btn_zip"]))  
 {  
      $output = '';  
      if($_FILES['zip_file']['name'] != '')  
      {  
           $file_name = $_FILES['zip_file']['name'];  
           $array = explode(".", $file_name);  
           $name = $array[0];  
           $ext = $array[1];  
           if($ext == 'zip')  
           {  
                $path = 'upload/';  
                $location = $path . $file_name;  
                if(move_uploaded_file($_FILES['zip_file']['tmp_name'], $location))  
                {  
                     $zip = new ZipArchive;  
                     if($zip->open($location))  
                     {  
                          $zip->extractTo($path);  
                          $zip->close();  
                     }  
                     $files = scandir($path . $name);  
                     //$name is extract folder from zip file  
                     foreach($files as $file)  
                     {  
                          $file_ext = end(explode(".", $file));  
                          $allowed_ext = array('jpg', 'png');  
                          if(in_array($file_ext, $allowed_ext))  
                          {  
                               $new_name = md5(rand()).'.' . $file_ext;  
                               $output .= '<div class="col-md-6"><div style="padding:16px; border:1px solid #CCC;"><img src="upload/'.$new_name.'" width="170" height="240" /></div></div>';  
                               copy($path.$name.'/'.$file, $path . $new_name);  
                               unlink($path.$name.'/'.$file);  
                          }       
                     }  
                     unlink($location);  
                     rmdir($path . $name);  
                }  
           }  
      }  
 }  
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Webslesson Tutorial | How to Extract a Zip File in Php</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>  
           <br />  
           <div class="container" style="width:500px;">  
                <h3 align="">How to Extract a Zip File in Php</h3><br />  
                <form method="post" enctype="multipart/form-data">  
                     <label>Please Select Zip File</label>  
                     <input type="file" name="zip_file" />  
                     <br />  
                     <input type="submit" name="btn_zip" class="btn btn-info" value="Upload" />  
                </form>  
                <br />  
                <?php  
                if(isset($output))  
                {  
                     echo $output;  
                }  
                ?>  
           </div>  
           <br />  
      </body>  
 </html>  

2 comments:

  1. Hello,

    I have learned a lot from your video. I have tried Upload and Extract a Zip File in Php. But, I have error message like this "Warning: scandir(upload/pic,upload/pic): The system cannot find the file specified." I just copied your code. Could you tell me what is wrong?

    ReplyDelete
  2. thanks a lot , but I have a question : what about this class : $zip = new ZipArchive; ??

    ReplyDelete