Unzip file using php code

Use the following code to unzip the zip file using php code:

<?php
function unzip($location,$newLocation){
if(exec(“unzip $location”,$arr)){
mkdir($newLocation);
chmod($newLocation,0777);
for($i = 1;$i< count($arr);$i++){
$file = trim(preg_replace(“~inflating: ~”,””,$arr[$i]));
chmod($file,0777);
copy($file,$newLocation.’/’.$file);
//
unlink($file);
}
return TRUE;
}else{
return FALSE;
}
}
?>
//Use the code as following:
<?php

if(unzip(‘zip-file.zip’,’unZipFile’))
echo ‘Success!’;
else
echo ‘Error’;
?>

Hope that this will be useful.

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online ... Contact : [email protected]

You may also like...

1 Response

  1. If you interested to write an article on 9lessons as a guest author plz send the document.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.