I'm using XAMPP to test a mini-website - I have some zip files I'd like to be able to place on the server, then for them to be unzipped using PHP via a browser.

How can I do this?

I've tried the following...

<?php
     $zip = new ZipArchive;
     $res = $zip->open(’zipfile.zip’);
     if ($res === TRUE) {
         $zip->extractTo(’extracted/’);
         $zip->close();
         echo ‘ok’;
     } else {
         echo ‘failed’;
     }
?>

and several other suggestions I've found around the net, but none of them work.

any help much appreciated!

ok, so I've tried the following, and it works using a small test zip file and this code alone...

<?PHP
// create object
$zip = new ZipArchive();

// open archive
if ($zip->open('zipfile.zip') !== TRUE) {
    die ("Could not open archive");
}

// extract contents to destination directory
$zip->extractTo('extracted/');

// close archive
// print success message
$zip->close();
echo "Archive extracted to directory";
?>

however, when incorporating it into the actual web page I'm trying to make, I get the error: 'Warning: ZipArchive::extractTo() [ziparchive.extractto]: Invalid or unitialized Zip object' - it creates the directory I have specified for the contents to be unzipped to, but doesn't actually extract and put anything in that directory. The code I'm using is as follows...

$dir = new DirectoryIterator('ZipFiles');

            foreach($dir as $file)
            {
            	$filename = $file->getFilename();

            	if(stristr($filename, ".zip") != null)
            	{
            		$zip = new ZipArchive();
            		$zipFilename = substr($filename, 0, -4);
            		echo $zipFilename;
            		echo $filename;

            		if($zip->open($filename) != TRUE) {
            			die ("Could not open archive");
            		}

            		$zip->extractTo('extracted/' . $zipFilename . '/');

            		$zip->close();
            		echo "Archive extracted to directory";
            	}
            }

... basically I am trying to scan through a directory, looking for all zip files, extracting the contents of each one found to a directory with the same name as the zip file (excluding the .zip). I'm still quite new to PHP.

any help with this error would be much appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.