2

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

opraz17
  • 75
  • 1
  • 3
  • 9

3 Answers3

1

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
Xavi
  • 196
  • 2
  • 9
  • I've got the error number 19 it's mean its not zip archive but no, the file is zip archive and placed on the same directory with the script – opraz17 Jun 17 '13 at 07:23
  • What is your php version ? have you tried with another zip files ? there is some php version with a bug that you can't open zip files with more size than 1.5 gb ~ https://bugs.php.net/bug.php?id=55383 – Xavi Jun 17 '13 at 07:32
  • 1
    Problem solved , previously I create the zip file with fwrite command , but when i try to create manually the zip file with winrar , its work , the problem is the zip file i created from fwrite command is not actually a zip file thanks for the advice to check the error code – opraz17 Jun 17 '13 at 07:49
0

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.

PleaseStand
  • 31,641
  • 6
  • 68
  • 95
0

you are declaring your $zip (ziparchive) incorrectly

<?php
$zip = new ZipArchive;

is missing parentheses

<?php
$zip = new ZipArchive();
if ($zip->open('data.zip')) {
tony gil
  • 9,424
  • 6
  • 76
  • 100