简体   繁体   中英

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

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

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.

you are declaring your $zip (ziparchive) incorrectly

<?php
$zip = new ZipArchive;

is missing parentheses

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM