Friday 17 June 2016

How to extract Zip Files with PHP?

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.
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);
?>

0 comments:

Post a Comment