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.