PHP: Unable to extact files from a ZIP archive

I am trying to upload a .zip file to a Server and extract it.

First I move the uploaded .zip file to my $target_path

$target_Path = $path . basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $target_Path);

Then I am trying to unzip the .zip file using ZipArchive in PHP (ZipArchive)

 function unzip($zipFile, $destination) {
                $zip = new ZipArchive();
                $zip->open($zipFile);
                for($i=0; $i<$zip->numFiles; $i++) {
                    $file=$zip->getNameIndex($i);
                    if(substr($file,-1) == '/') continue;
                    $lastDelimiterPos = strrpos($destination.$file, '/');
                    $dir = substr($destination.$file, 0, $lastDelimiterPos);
                    if (!file_exists($dir)) {
                        mkdir($dir, 0777, true);
                    }
                    $name = substr($destination.$file, $lastDelimiterPos + 1);
                    echo $dir. "/" .$name;
                    copy("zip://$zipFile#$file","$dir/$name");
                }

                $zip->close();
            }

            unzip($target_Path, $path);

$target_path is a relative path directly to the .zip file

$path is a relative path to the folder ending in a “/”

Through the Error-Message I know, that my path to the .zip file must be correct (I see the files I want to copy)
The Error I get:
copy(zip://…/Customer/Test/Screen Links/HTML_Content/WETTERKARTE.zip#WETTERKARTE/alt/fs_panel.svg): Failed to open stream: operation failed in D:\xampp\htdocs\MVM_RED_VIOLET\php\AX_upload_PIC.php on line 60
So I know that fs_panel.svg (the first file in the ZipArchive) gets found.

I just don´t know how I can copy the file from the ZipArchive to an folder outside.

I also tried zip->extractTo - that only gives me no Error but also no files - it just says it worked without doing anything.

The issue you’re facing with the ZipArchive likely stems from the fact that copy() isn’t the correct approach for handling files within a zip archive. Instead, ZipArchive provides a dedicated method to extract files, which is extractTo().

Here’s a more reliable way to unzip files using ZipArchive:

php

Copy code

function unzip($zipFile, $destination) {
    $zip = new ZipArchive();
    
    // Try to open the zip file
    if ($zip->open($zipFile) === TRUE) {
        // Extract all files to the destination directory
        if ($zip->extractTo($destination)) {
            echo 'Extraction successful';
        } else {
            echo 'Extraction failed';
        }
        $zip->close();
    } else {
        echo 'Failed to open the zip file';
    }
}

// Define paths
$target_Path = $path . basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $target_Path);

// Unzip the file
unzip($target_Path, $path);

Key Points:

  1. extractTo($destination): This method automatically extracts the entire contents of the zip file to the $destination directory. There’s no need to manually iterate over the files or use copy() for each file.
  2. Permissions: Ensure that the target directory ($path) has proper write permissions (e.g., 0777) to allow the zip files to be extracted.
  3. Zip File Path: The $zipFile should be the full path to your zip file, and $destination should be the directory where you want to extract the files.

Debugging Permissions:

If the extraction still doesn’t work, ensure the web server has the necessary permissions to write to the folder. You can try running:

php

Copy code

if (!is_writable($path)) {
    echo "The folder is not writable.";
} else {
    echo "The folder is writable.";
}

This will confirm whether the destination folder can be written to. If not, you may need to change the permissions using chmod or adjust them via your server’s file system.