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.


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

    // 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;


    if ($found == FALSE)
        echo "No files ending in .zip found<br>";
        echo '<br>Warning: Existing files will be overwritten.<br><br><INPUT type="submit" value="Unzip!">';

    echo "</FORM>";
Your email address will not be displayed
Avatars from https://gravatar.com/ are connected to your provided email address.
Notify me about replies:  
Oldest Most Voted
Inline Feedbacks
View all comments