Unzip file using php code

in PHP Code / 1 Comment

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.

With more than 8 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]

Discussion 1 Comment

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>