Many web hosts use CPanel, as it offers a fairly intuitive way for people to manage their accounts. CPanel comes with a file manager that is functional, but somewhat clunky. Although CPanel offers a mechanism for extracting archives (.zip and .gz), this mechanism has one major downside: when extracting .zip files, currently existing files will not be overwritten. This makes upgrading software that is distributed in .zip packages very difficult.
To work around this limitation, I wrote a PHP script that allows users to extract .zip files and overwrite current files. It presents the user with a list of all the .zip files in the directory, and allows him/her to submit one for extraction. The whole process is quite intuitive. Here is what the interface looks like:
Please choose a file to unzip:
The selected file is then unzipped.
To use the script, simply follow this procedure:
1) Upload this script via FTP (or paste the script into a new file) in the directory that contains the .zip file to unzip.
2) Run your web browser, and point it at the script
3) Select the file to unzip, and hit the “Unzip” button.
That’s it!
Make sure you delete the script after running it, as leaving it on the machine could result in a security issue.
unzip.php:
<?php
// The unzip script
// Created by Alex at http://www.learncpp.com
//
// This script lists all of the .zip files in a directory
// and allows you to select one to unzip. Unlike CPanel's file
// manager, it _will_ overwrite existing files.
//
// To use this script, FTP or paste this script into a file in the directory
// with the .zip you want to unzip. Then point your web browser at this
// script and choose which file to unzip.
// See if there's a file parameter in the URL string
$file = $_GET['file'];
if (isset($file))
{
echo "Unzipping " . $file . "<br>";
system('unzip -o ' . $file);
exit;
}
// create a handler to read the directory contents
$handler = opendir(".");
echo "Please choose a file to unzip: " . "<br>";
// A blank action field posts the form to itself
echo '<FORM action="" method="get">';
$found = FALSE; // Used to see if there were any valid files
// keep going until all files in directory have been read
while ($file = readdir($handler))
{
if (preg_match ("/.zip$/i", $file))
{
echo '<input type="radio" name="file" value=' . $file . '> ' . $file . '<br>';
$found = true;
}
}
closedir($handler);
if ($found == FALSE)
echo "No files ending in .zip found<br>";
else
echo '<br>Warning: Existing files will be overwritten.<br><br><INPUT type="submit" value="Unzip!">';
echo "</FORM>";
?>
Did you know that you can get a C++ programming degree online? There are many online schools that offer computer programming classes and courses. It's easy to get your online degree, so start studying up on something you love!

Question. I tired this and it runs but it doesn’t unzip the file. What do you suggest?
Hi Mark. This script calls an executable file named “unzip” to do the actual work of unzipping the file. It was set up to work on a linux system. If your linux system does not have unzip, or you are running on another type of system, then the script won’t do anything.
If you know the name of an executable that will do the unzipping on your system, you can replace the word ‘unzip’ in the following line:
system('unzip -o ' . $file);with whatever executable file does the unzipping on your target system. As to what that executable might be on your system, I can not say since I don’t know what your target environment is. Google might be of assistance in this regard.
It was a permisson issue. The folder it’s in needs to be chmod 777 and I had it as 755. Thanks for a great script!
That is odd. It works fine for me when I test it in a 755 folder. In any case, glad you were able to get it working.
Thank you very much.
Hi there Alex,
I’m having problems with the script. It just says Unzipping Filename.zip and thats it. Does that mean it has already done it?
Thanks.
Best Regards,
John
Okay I tested it,
But it doesn’t unzip anything :S.
Thanks
Plus I’m hosted on a Linux System :D
Hi John. As mentioned in the comments above, The line that does all the real work is this one:
system(’unzip -o ‘ . $file);
If it doesn’t work on your system/host, I’m not sure what to tell you. Probably either the unzip executable doesn’t exist, or the system command is blocked (if that’s possible). My guess is the former.
Oh Okay, Thanks Alex, well can I contact you via MSN or AIM or Yahoo or Google Talk if you use any of these services? If you do, please provide it to me here or send me an email to private_server@hotmail.com with your ID :D.
Thanks very much.
John
In Bluehost.com can use this script. In hosting that use control panel named “Cpanel 11″, It has functions called “Extract” and “Compress” to compress and extract files and folders.
Excellent script. Worked first time.
cPanel take note, this should be incorporated into the next upgrade…
Hope they pay you for it Alex..
Congrats..
You are awesome! The code worked wonderfully and did what I needed with about 2 minutes of my time. All hail Alex! Seriously, thanks.
works great except for one main flaw. once you unzip a file, it is not possible to delete the files that were unzipped unless you do it via ssh. i believe it is cause it is not sure who the owner is being unzipped that way, so, it thinks it is the root user. please let me know if you have a fix for this.
wow the script works great! but i have a question. what if i will unzip my file to a particular directory? what would be the additional for this code?
thanks… noister
$execute = system(”unzip -o ../includes/themes/$fileName -d ../includes/themes/”);
`$execute`;
Noister, if the file being unzipped has included subdirectories, the files will be put in those subdirectories. However, if you wish to unzip to another directory entirely, you’ll have to modify the unzip command line to do that.
There’s some information about unzip here.
Looks like adding “-d yourdir” to the end of the unzip command line should work.
still having the problem of php not thinking the new directory existing. cant delete via ftp, only through ssh cause it says the file isnt there. i have tried putting full root path, and that makes it so it doesnt wort at all. any ideas?
I haven’t had any trouble deleting file I’ve unzipped via this utility via ftp or otherwise, so I’m not sure what to tell you. It sounds like a permissions issue, but I’m not sure even where to begin looking for something like that… Have you tried a different FTP client?
get error - no file ending zip found
That means it didn’t find any files with the .zip extension in the directory you ran the script in.
For those not able to get this working because of
system('unzip -o ' . $file);try
shell_exec('unzip -o ' . $file);Worked for me :)
Thank you very much! I appreciate your submission. :) God-bless Google.
thanks! worked great!
Alex,
Grrr8 script! Thanks for creating/writing it!
One question, if I may…
What would be the modification required to have the unzipped files to be placed into a folder named for the zip file that was unzipped?
Thanks again!
ECS Dave
I just want to thank you for saving a few hours of my Saturday! What a useful little time-saver. It would have taken several hours to upload ~1000 files via ftp (on my satellite connection)and I suck at shell commands, so this was the perfect thing. Instead, I had my site upgraded in a few minutes.
Many, many thanks!
William
Great for me, too. However, two questions:
1.) The script now lists awful lot of files which are extracted. Can I suppress the output?
2.) Is there a return value so that I can find out whether the extraction was successful or not? (e.g. broken archives, checksum errors)
Thank you.
Thanks For The Very Good Script
But One Problem
The Extracted Files Shows Owner And Group As “httpd:httpd”
So I Cannot Change The Permission Of Extracted Files And Have Some Other Problems As Files Are Owned By httpd
Is There Something In Script I Can Add/Modify So That Files Are Not Owned By httpd.
Thanks
It is working on linux i want it for windows,can anyone help me in this regard.
Thanx
Hi..
the zip file is stored in the file system.
How if the file is stored in a database as blobs?
thanks
Hi Alex! Please make me an unzipper script(I will use it in my wap-site)…In the first page(html) to apperar a field where must be wrotten the adress of the zip…and a second php page where to appear the unzipped files and their size (eg. http://razvan.wen9.com/Pagini/Utile/Unzip.html , but I have just the html, because the php is on another server, wich loads slowly :( ) . Send the script to razwan_z@yahoo.com . Thanks!
I’ve got the following error when running the script
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster@sourcedns.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Apache/1.3.41 Server at servG0GBVTCF.sn.sourcedns.com Port 80
Thanks
Alex, many thanks for taking the time to post this — worked perfectly for me. And I will be silently thanking you every time I update our Web site with the latest ZIP full of files that would take all day to upload here, there, and everywhere manually!
Bill
Great work!
The good thing that the script can unzip huge files! I’ve got a problem to load and unzip 120 Mb site, and finally discovered the cure!
Thank you!
S
I get
Notice: Undefined index: file in C:wampwwwunzipuni.php on line 14
php 5.2.8
this is line 14
Wow! This is awesome! It worked great as I have a lot of clients with Joomla updates. This will help me greatly!! I would recommend it for installing Joomla updates!
Thanks!
Michelle