Uzipping Files on Free Web Servers


If you are using a free hosting account with or other free hosts out there, you may have found that they don't offer an easy way to unzip your files on the server. Uploading zip files is faster than uploading raw files, plus it takes less time.

Thus, I wrote a PHP script to unzip files on a hosting account with no zip program. This is a quick and dirty hack that does not require a lot of knowledge of how zipfiles work, and the truth is the first example here really has less to do with how to use the php programming language and more to do with how to send system commands to the underlying Linux system using a php script. MAY have added a zipping feature since I wrote this tutorial. Even is so, this information is still useful for any hosting account that may lack a zip file feature. Oftentimes, I have a domain that is for testing purposes, so it really makes financial sense to take advantage of the free hosting accounts that includes with each domain purchase.

Unzipping Files On Linux

My first example is on a Linux/Unix system running an apache server.

Unzipping files on a Linux system is easy. Linux comes with several zipping and unzipping programs that you can call up with the standard input(keyboard).

I noticed recently when I tried to upload a script to a Godaddy Account that via FTP that the script would be rejected if it contained the php "system" function.

It looks to me like Godaddy is flagging it as malicious. It is probably a good thing really because the system function is the same as being on the command line.

Here is the code example anyway - your particular situation may be different than mine.

system( 'unzip');

Viola!!! Its that simple. Nothing to it. If your zip File is not corrupted and as long you have permissions on your server - it will unzip with no problems - printing to screen a list of the files that were extracted

The "system" command is just a way to send commands out to the server's operating system. You can actually use "system" to run just about any program that can be found on the underlying operating system that is hosting your account if your account has the proper permissions.

Unzipping Files on Windows and Other Servers without Zip.

Using ZipArchive Class

Some servers may not have a built-in zip program or you may not have permission to use the system function.
So, we will do a little scripting to to learn how to unzip - zip files on these servers.

$zip = new ZipArchive;
if ($zip->open('') === TRUE)
{ $zip->extractTo('/my/destination/dir/');
$zip->close(); echo 'ok'; }
else { echo 'failed'; }

I found the above example in the php manual.
I did not see how I could possibly improve on the example, so I left it as is.
I do, however, make every attempt to give credit to the correct authors when ever possible.

Author: D.Shaun Morgan