Stack Overflow Asked by Ciro García on December 7, 2021
I have a folder in my web server were I put zip files that I need to then unzip. I want to do that with php and this is what I have tried but it does not work:
<?php
$file = $_GET["file"];
$zip = new ZipArchive;
$res = $zip->open($file+'.zip');
$zip->extractTo('./');
$zip->close();
?>
The zip files are in the same folder as the php file, but when I go to the php page it does nothing.
By doing some testing I have found out that the script dies on the $zip = new ZipArchive;
line
How can I manage this to work?
I have found the problem. The code is fine, but the hosting service is not, and they do not have the ZIP extension available right now
Answered by Ciro García on December 7, 2021
<?php
$fileName = $_GET['file']; // get file name in the URL param "file"
if (isset($fileName)) { // if $fileName php variable is set than
$zip = new ZipArchive; // create object
$res = $zip->open($fileName); // open archive
if ($res === TRUE) {
$zip->extractTo('./'); // extract contents to destination directory
$zip->close(); //close the archieve
echo 'Extracted file "'.$fileName.'"';
} else {
echo 'Cannot find the file name "'.$fileName.'" (the file name should include extension (.zip, ...))';
}
}
else {
echo 'Please set file name in the "file" param';
}
?>
Note:- For More Details Please refer https://www.php.net/manual/en/class.ziparchive.php
Answered by KUMAR on December 7, 2021
Try this code. Also change $zip->open($file+".zip");
to $zip->open($file);
.
+ (plus sign) is not concatenation operator in php
<?php
// $_GET["file"] is set to `a.zip`
$file = $_GET["file"];
$zip = new ZipArchive;
$res = $zip->open($file);
$zip->extractTo('./');
$zip->close();
?>
Answered by Chilarai on December 7, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP