简体   繁体   中英

PHP ZipArchive extractTo issues

So, I'm trying to create a bulk upload feature where a user can upload a single zip file containing a bunch of PDFs which will then get processed and stored on the server etc.

So far I have this...

$serverpath = $_SERVER['DOCUMENT_ROOT'];

if(array_key_exists('bulk_filename', $_FILES)){
    if ($_FILES['bulk_filename']['error'] === UPLOAD_ERR_OK) {

        $file_name = $_FILES['bulk_filename']['name'];
        $new_zip_file = $serverpath . '/customerdata/tmp_invoices/' . $file_name;
        move_uploaded_file($_FILES['bulk_filename']['tmp_name'], $new_zip_file);

        // zip file is coming in as "-rw-r--r--" and should be "-rwxr-xr-x"
        exec('chmod -R 755 ' . $serverpath . '/customerdata/tmp_invoices/' . $file_name);

        // Extract the files from zip
        $zip = new ZipArchive;
        $res = $zip->open($new_zip_file, ZipArchive::OVERWRITE);
        if ($res !== true) {
            die("Cannot open {$new_zip_file} for writing!");
        }
        else{
            $res = $zip->extractTo($serverpath . '/customerdata/tmp_invoices/');
            $zip->close();

            ////////////////////////////////////////////
            // I ALWAYS REACH THIS CODE BLOCK
            // and $res always equals 1/true
            ////////////////////////////////////////////
        }
    } else
        die("Upload failed with error code " . $_FILES['bulk_filename']['error']);
}

The problem I'm seeing is that even though the extractTo function returns as a successful extraction (1), I do not see the files that have apparently been extracted?!

I always see the Archive.zip file so I know that is being uploaded correctly...

-rwxr-xr-x 1 nginx nginx 68512374 May  4 12:39 Archive.zip

Any ideas as to what I'm doing wrong?! :-/

ENVIRONMENT: Centos 6 running PHP 5.3.3

UPDATE

The below (with fully qualified path) throws no errors, just doesn't seem to do any sort of extraction?!

$zip->extractTo($serverpath . '/customerdata/tmp_invoices/');

The below (with relative path), throws a Warning: ZipArchive::extractTo(): Permission denied in... error?!

$zip->extractTo('/customerdata/tmp_invoices/'); 

Cheers guys

For those of you that find this question due to having similar issues...

I managed to "fix" this issue by changing the second argument:

FROM THIS : $zip->open($new_zip_file, ZipArchive::OVERWRITE)

TO THIS : $zip->open($new_zip_file, ZipArchive::CREATE)

php.net

ZipArchive::CREATE (integer)

Create the archive if it does not exist.

ZipArchive::OVERWRITE (integer)

Always start a new archive, this mode will overwrite the file if it already exists.

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