Category:php

Uzipping Files on Your Web Server

If you are using a free hosting account with godaddy.com and many of the other Free hosts out there, you may have found that they don't offer you a lot of features. One feature that I could not find on Godaddy.com free hosting accounts was a utility that would unzip my files after I uploaded them to my server. I had a dating site software that I was trying to install after I uploaded the zip files to my hosting server.

Thus, I wrote a PHP script to unzip my software. This is a quick and dirty hack, 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.

Godaddy.com 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 Godaddy.com 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).

<?php system('unzip myZipFile.zip'); ?>

Viola!!! Its that simple. Nothing to it. If your zip File is not corrupted it will deflate 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

Windows does not come with a similar unzip program.

So, we will do a little phping to accomplish this task

<?php $zip = new ZipArchive; if ($zip->open('test.zip') === TRUE) { $zip->extractTo('destination/directory/'); $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. I could have added a lot of fluff and loops to the code but that would only make the script more complicated than necessary. D.Shaun Morgan

Author: D.Shaun Morgan