Unzip a Zip File in PHP

Here is the code to unzip a Zip file using php and store it to another location.

<?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;
	}
}

if( unzip('zipedfiles/test.zip', 'unziped/myNewZip') ) {
	echo 'Success!';
} else {
	echo 'Error';
}
?>

Leave a Reply