How to extract Zip Files with PHP?

In the previous post, we have created a random alphanumeric password generator script. In this post, I will show you, how to extract zip/compressed files using PHP. PHP has built-in extensions for dealing with compressed files.

Quick Tip : Simple PHP Password Generator

Following PHP function can be used to unzip the compressed files. It accepts two parameters, source i.e full path to zip file and destination i.e path to which files will be extracted.

<?php
//extract a file with php
function extractZip($src,$dest){
	$zip = new ZipArchive;
	$res = $zip->open($src);
	if ($res === TRUE) {
		$zip->extractTo($dest);
		$zip->close();
		echo 'Files Extracted Successfully!';
	} else {
		echo 'Extraction Failed!';
	}
}

$src = "zip/file.zip";
$dest = "extractedfile/1/";
echo extractZip($src,$dest);
?>

Author: rdlowrey

Source: Stack Overflow

1 Comments

Leave a Comment.