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:

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.


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

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

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


  • 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, 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 Port 80


  • 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. , but I have just the html, because the php is on another server, wich loads slowly :( ) . Send the script to . Thanks!

  • Achmed Steinweg

    the zip file is stored in the file system.

    How if the file is stored in a database as blobs?


  • Paramita

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


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

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

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

  • 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

  • Thank you very much! I appreciate your submission. :) God-bless Google.

  • Greg

    For those not able to get this working because of


    Worked for me :)

  • get error - no file ending zip found

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

  • 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/");

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

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

  • You are awesome! The code worked wonderfully and did what I needed with about 2 minutes of my time. All hail Alex! Seriously, thanks.

  • chick100

    Excellent script. Worked first time.

    cPanel take note, this should be incorporated into the next upgrade...
    Hope they pay you for it Alex..


  • Ritz

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

  • 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 with your ID :D.

    Thanks very much.


  • 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

    Plus I'm hosted on a Linux System :D

  • John

    Okay I tested it,

    But it doesn't unzip anything :S.


  • John

    Hi there Alex,

    I'm having problems with the script. It just says Unzipping and thats it. Does that mean it has already done it?


    Best Regards,

  • Ritz

    Thank you very much.

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

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

  • 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

    Question. I tired this and it runs but it doesn't unzip the file. What do you suggest?

Leave a Comment

Put all code inside code tags: [code]your code here[/code]