Php Flow » Php

How to UnZIP a File

How to unzip a file in php.this is very general problem when the user wants uploaded zip should be extract.
We will create method for unzi the file.This method has two argument one is source location other is destination location.

  1. <?php  
  2.     function unzip($location,$newLocation){  
  3.         if(exec(“unzip $location”,$arr)){  
  4.             mkdir($newLocation);  
  5.             for($i = 1;$icount($arr);$i++){  
  6.                 $file = trim(preg_replace(“~inflating: ~”,“”,$arr[$i]));  
  7.                 copy($location.‘/’.$file,$newLocation.‘/’.$file);  
  8.                 unlink($location.‘/’.$file);  
  9.             }  
  10.             return TRUE;  
  11.         }else{  
  12.             return FALSE;  
  13.         }  
  14.     }  
  15. ?>  

Use the code as following:

  1. <?php  
  2. include ‘functions.php’;  
  3. if(unzip(‘zipedfiles/test.zip’,‘unziped/myNewZip’))  
  4.     echo ‘Success!’;  
  5. else  
  6.     echo ‘Error’;  
  7. ?>  

Did you enjoy this article? Share it!

About the Author:

Hi, This is Parvez Alam from India. I am software developer with 4 years’ experience in web development. I have submitted articles on PHP, Mysql, Magento,CSS, HTML, jQuery, web designing and social API. You can subscribe to my blog via RSS/Twitter/Google plus and Facebook.

Random Posts

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>