Server Side Un-Zipping Files

I am back from a super long hiatus and would like to say that I have had a lot of things going and haven’t been able to focus on writing. Hopefully, I will be able to post more frequently again, now. That said, I have had many people ask me over the course of the past year how they can unzip a file on their server. The main benefit to this is, because we live in a rural area, our only option for internet access is satellite speed internet.

It takes a lot less time with FileZilla to upload a single .zip file to the server and extract the contents versus uploading each file. This also saves bandwidth.

To do this, you will need to create two files. I name the files u.php and unzip.php. u.php will be the file that you will actually want to load in your browser first. It is simply a form that will allow you enter the folder name. unzip.php will do the work of extracting the file.

So, let’s take a look at the code:

u.php

<form method="post" action="unzip.php">
<input name="name" size="60" placeholder="Folder_Name"/>
<input type="submit" value="submit" />
</form>

As you can see, u.php is simply a submission form that will allow you to submit your information to the next file which will extract the contents of the file.

unzip.php

<?php
$name = $_POST['name'];
?>
<title><?php echo $name; ?> unzipped</title>
<?php
// increase script timeout value
ini_set("max_execution_time", 600);
//mkdir($name);

$zip = new ZipArchive;
$res = $zip->open($name.".zip");
if ($res === TRUE) {
$zip->extractTo($name."/");
$zip->close();
echo ‘ok’;
} else {
echo ‘failed’;
}
?>

The unzip.php file uses ZipArchive to extract the contents of the file and create a folder of the same name. If you wish to have the contents at the root level, simply move the files with an FTP client.

I, personally, use this script everytime I install WordPress or other php scripts on my shared hosting server. This reduces time and bandwidth because I already have the .zip file uploaded. I simply upload u.php and upload.php to the server and move the .zip file to the folder where I wish to install the script.

Be sure that if you use this script, remove the files once you are through using them!

Comments

comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.