Unzip using php code

Code examples and user hints and tips
Post Reply
studgomel
Site Admin
Posts: 31
Joined: Wed Feb 27, 2019 3:48 am

Unzip using php code

Post by studgomel » Wed Mar 06, 2019 2:04 am

:evil:
Hi,

following code is to unzip a zip file using php code:

Code: Select all

<?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 
//include 'functions.php'; 
if(unzip('zip-file.zip','unZipFile')) 
echo 'Success!'; 
else 
echo 'Error'; 
?>


The php zip-file.zip and unzip file is in the unZipFile folder.

Hope that it is useful.

Thank You,