I’m attempting to upload a .zip file to a server and extract its contents using PHP.
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);
Next, I try to unzip the .zip file with the ZipArchive class:
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);
In this code, $target_path points directly to the .zip file, while $path is the relative path to the folder ending with a /.
From the error message, I know that the path to the .zip file is correct since I can see the files I want to copy. However, I’m encountering the following error:
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
This indicates that fs_panel.svg (the first file in the ZipArchive) is being found, but I can’t copy the file from the ZipArchive to an external folder.
I’ve also attempted using $zip->extractTo(), but it runs without error and does not extract any files.
I’m looking for assistance in resolving this issue.