Extract a zip file using php

Published


Here is how you can extract a .zip file using php on your server.
Change zipfile.zip to the name of your zip file.
Declare the path ,the contents to be extracted to. if not specified, it will be in root directory

<?php
$zip = new ZipArchive;
$res = $zip->open('zipfile.zip');
if ($res === TRUE) {
  $zip->extractTo('./');
  $zip->close();
  echo 'Success!';
} else {
  echo 'Failed';
}
?>

Code Explained


<?php
$zip = new ZipArchive; //Storing the filetype and format to variable to $zip.
$res = $zip->open('zipfile.zip');  //Trying to open .zip file.
if ($res === TRUE) { //If Opening .Zip file is success,
  $zip->extractTo('./'); //extract the contents to specifed folder.
  $zip->close(); //Closing file.
  echo 'Success!';//Dispaling text "success!" on the page.
} else { //If Opening of .zip file fails, then
  echo 'Failed';// Displaying a message "Failed". on the page.
}
?>



Comments

Post a Comment