(Yet another) PHP Unzip code

Basically it’s just a “merge” of this and this combining the best of both (the second one rely on a system(‘zip’) call which often a standard php hosting doesn’t allow).

 

<?php
    // The unzip script
    // Created by Max at http://www.solvedo.com
    //
    // This script lists all of the .zip files in a directory
    // and allows you to select one to unzip. 
    //
    // To use this script, FTP or paste this script into a folder where you can
    // invoke it remotey (i.e. www.mysite.com/unzip.php"
 
    // TO CUSTOMIZE: Directory where to read and unzip the files
    // N.B. Unzip will be done in the '$workdir/zipfilename.zip.tmp/' folder.
    $workdir = 'tmp/';
	
    // See if there's a file parameter in the URL string
    $file = $_GET['file'];
 
    if (isset($file)) {
		echo "Unzipping " . $file . "
"; $zip = new ZipArchive; $res = $zip->open($workdir.$file); if ($res === TRUE) { if ($zip->extractTo($workdir.$file.'.tmp/')){ echo "Unzip done."; } else { echo "Unzip failed."; } $zip->close(); } else { echo "Failed to open file."; } exit; } // create a handler to read the directory contents $handler = opendir($workdir); echo "Please choose a file to unzip: " . "
"; // A blank action field posts the form to itself echo "
"; $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 " " . $file . "
"; $found = true; } } closedir($handler); if ($found == FALSE) echo "No files ending in .zip found
"; else echo "
Warning: Existing files will be overwritten.

"; echo "
"; ?>

One thought on “(Yet another) PHP Unzip code

  1. Hi,

    have you customized the $workdir script parameter?
    The script will look in the $workdir folder for the existing zip files, no matter where the script is located.

    Let me know if this helps.
    Max

Comments are closed.