How to

Extract files from zipped file using PHP

Extracting a file from a compressed zipped file is almost easy while working on Offline mode in Windows, Mac or Linux operating system. When you have a same view on Web, you need to write a PHP Program or using any other Computer languages. On my view, PHP is the best Language to do such actions.

This method can be used when you want the users to upload a file that need to be extracted on the same web server. After Extraction, you can send the file for processing or verification or whatever you want to do with that file.

Open your favorite text editor like WordPad which supports PHP format to be saved.

Create a file technostreak.php (or with any other name. The extension .php is important here. Also if you change the name, you will need to modify the upcoming codex)

Copy the below code to the file you created.

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

Save the file and create another new file. Only with this file you will be able to run the above code. In this file, you need to specify the file location of the zipped file. Also if you have saved the above code with some other name, you will need to update here. Copy the below code in the file yo have created.

<?php
include 'functions.php';
if(unzip('zipedfiles/test.zip','unziped/myNewZip'))
 echo 'Success unzipping the file!';
else
 echo 'Error unzipping.Check the file!';
?>

Save the file you created as myname.php

In the above code, you must specify the location of your zipped file. Replace zipedfiles/test.php with the location of the zip file that you need to extract.

Just Run the file

Thank you! Share your thoughts and experiences in comments. 🙂

About the Author

I am interested towards computers since my childhood days. I can't be without my computer and blogging works. I blog at CryptLife. I love to be in front of my PC, managing my blog every day, every minute and every hour.

1 comments
Animals and Livestock
Animals and Livestock

Thanks a lot for shearing such a helpful information. As in I am doing crash course of PHP.