Unzip Zip File

Submitted by:David Villa

Date added:30 March, 2011

Category:PHP

Need to unzip a file? this function can do that for you.

It is reccomended that the functions.php file and the file that exicutes the unzip function are NOT in the same directory as the zip files.

unzip ( string $zipfile , string $newLocation )

zipfile gets the name of the current zip file.
newLocation is where you want to store all the information that is in the zip file.

Tags: unzip file , php unzip

Code Snippet:

functions.php :
==========

<?php
function unzip($location,$newLocation){
if(exec("unzip $location",$arr)){
mkdir($newLocation);
for($i = 1;$i< count($arr);$i++){
$file = trim(preg_replace("~inflating: ~","",$arr[$i]));
copy($location.'/'.$file,$newLocation.'/'.$file);
unlink($location.'/'.$file);
}
return TRUE;
}else{
return FALSE;
}
}
?>



extractZip.php :
===========

<?php
include 'functions.php';
if(unzip('zipedfiles/test.zip','unziped/myNewZip'))
echo 'Success!';
else
echo 'Error';
?>
 
 

Comments