opraz17
opraz17

Reputation: 85

Unzip The Zip Archive With PHP

I Have A Problem About extract the zip file with PHP
I try many shared script on the web but it still doesn't work
the last script i try is this script :

<?php
$zip = new ZipArchive;
$res = $zip->open('data.zip');
if ($res === TRUE) {
  $zip->extractTo('/extract/');
  $zip->close();
  echo 'woot!';
} else {
  echo 'doh!';
}
?>

I am always get the else condition when the script is run , I've tried replacing the data.zip and the /extract/ path to complete path http://localhost/basedata/data.zip and http://localhost/basedata/extract/ but I still got the else condition , Anyone can help me?

Here Is My whole script and the zip file http://www.mediafire.com/?c49c3xdxjlm58ey

Upvotes: 3

Views: 4836

Answers (3)

tony gil
tony gil

Reputation: 9554

you are declaring your $zip (ziparchive) incorrectly

<?php
$zip = new ZipArchive;

is missing parentheses

<?php
$zip = new ZipArchive();
if ($zip->open('data.zip')) {

Upvotes: 0

PleaseStand
PleaseStand

Reputation: 32082

ZipArchive uses file paths, not URLs.

For example, if your web server's document root is /srv/www, specify /srv/www/basedata/data.zip (or wherever the file is on the server's local file system), not http://localhost/basedata/data.zip.

If the .ZIP file is on a remote computer, change the script to download it first before extracting it, though this does not seem to be your case.

Furthermore, the user the PHP script runs as needs to have read permission for the Zip file and write permission for the destination for extracted files.

Upvotes: 0

Xavi
Xavi

Reputation: 306

You should check which error code gives open (http://www.php.net/manual/en/ziparchive.open.php), that will give you some help.

Error codes are this:

    ZIPARCHIVE::ER_EXISTS -10
    ZIPARCHIVE::ER_INCONS - 21
    ZIPARCHIVE::ER_INVAL - 18
    ZIPARCHIVE::ER_MEMORY - 14
    ZIPARCHIVE::ER_NOENT - 9
    ZIPARCHIVE::ER_NOZIP - 19
    ZIPARCHIVE::ER_OPEN - 11
    ZIPARCHIVE::ER_READ - 5
    ZIPARCHIVE::ER_SEEK - 4

Upvotes: 2

Related Questions