By 05/02/2014 6 Comments

How to Unzip a Zip File in PHP

What is Zip files?

  • It is a computer file, which has its contents in compressed form
  • It has .zip or zipx extension. It is also called archives
  • A zip file contains many files and  other compressed files
  • It helps us to keep several files together in one zipped folder or directory
  • If we need the files which is inside zip file format, we must extract or unzip these files
  • Following is PHP code which is used to unzip a zip file

Use this code snippet for unzip a zip file in PHP.

<?php

function unzip($location,$newLocation){ //create PHP function unzip which pass 2 parameters($location,$newLocation) first param is the actual zip file, second param is the destination location or destination path

if(exec("unzip $location",$arr)){ // condition pass a exec() function which has 2 parameters first specifies the command which will be executed, second is an output array which will be filled line by line from the command

mkdir($newLocation); // This function creates a directory for given name or path,pass 1 parameter which is the path or name for directory

for($i = 1;$i< count($arr);$i++){

$file = trim(preg_replace("~inflating: ~","",$arr[$i])); // Loop through array and get all lines from an array store these in $file variable and also strip whitespace from start and end

copy($location.'/'.$file,$newLocation.'/'.$file); // This function copy the actual file from source path to destination path 2 parameters passed first is the source path and second parameter is destination path

unlink($location.'/'.$file); // This function permanently delete the file of given path as a parameter 

}

return TRUE;

}else{

return FALSE;

}

}

/* Use above function */

if(unzip('zipedfiles/test.zip','unziped/myNewZip')) // This line calls the above function and pass 2 arguments to that function, first argument is actual file and second argument is the destination file

echo 'Success!';

else

echo 'Error';

?>

 







Posted in: PHP

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .