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:
<br>
copy(zip://../Customer/Test/Screen Links/HTML_Content/WETTERKARTE.zip#WETTERKARTE/alt/fs_panel.svg): Failed to open stream: operation failed in D:xampphtdocsMVM_RED_VIOLETphpAX_upload_PIC.php on line 60 <br>
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 a 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.
2
Answers
I could not get the zipArchive to work so I looked for other options...
I solved my problem using
shell_exec()
this command uses 7-Zip (which is installed on my Pc) and extracts a
$file
to my$path
Things you need to set for this to work:
variables_order="GPCS"
tovariables_order="EGPCS"
in your php.ini file2>&1
to the end of you commandthis setup executes any command you write in your php file in a shell - so if a command works in your shell it should work in php too
Try this: