Search

A PHP script to unzip files with file overwriting

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:

test.zip
test2.zip

Warning: Existing files will be overwritten.

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:

45 comments to A PHP script to unzip files with file overwriting

  • Mark

    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:

    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.

  • Mark

    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.

  • Ritz

    Thank you very much.

  • John

    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

  • John

    Okay I tested it,

    But it doesn’t unzip anything :S.

    Thanks

  • John

    Plus I’m hosted on a Linux System πŸ˜€

  • 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.

  • John

    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

  • Ritz

    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.

  • chick100

    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.

  • Justin

    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.

  • noister

    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

  • Greg

    For those not able to get this working because of

    try

    Worked for me πŸ™‚

  • Thank you very much! I appreciate your submission. πŸ™‚ God-bless Google.

  • 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

  • Thomas

    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.

  • CodeLab

    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

  • Paramita

    It is working on linux i want it for windows,can anyone help me in this regard.

    Thanx

  • Achmed Steinweg

    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!

  • Paulo

    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

  • Bill

    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

  • ben

    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

  • roy

    Thank you excellent script!!

  • Hi, Your coded worked for me. Its nice.

    I have downloaded the unzip utility from this link http://stahlforce.com/dev/unzip.exe and used it to unzip.

    Thanks..

  • Many thanks for this, I have spent hours trying to get this right, but your script worked straight a way.

  • its not working in mine it says

    Parse error: syntax error, unexpected T_DNUMBER in xxxx/unzip.php on line 3

  • Thanks for the great script. Saved me alot of time especially after having too much problems accessing cpanel due to low bandwith.

  • paul

    worked perfect thank you so much

  • Hi,
    I get following error while try to unzip file

    Warning: system() has been disabled for security reasons in /home/a5204067/public_html/joomla/unzip.php on line 19

    What could be the reason. I change permission to 777 to all file and folder but does not work.
    Please helpme.

  • michelbg

    Muchas gracias!! Funciona perfecto en DreamHost.
    Existe algΓΊn script que haga lo contrario?
    Es decir, que pueda seleccionar directorios y archivos y los comprima en un zip.

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter