For some reason I can't seem to get the following code to work. I have a form which accesses it, and I've passed a .zip file through it but still no luck? Any clues?
<?php
$file = $_POST['zipped_file'];
$zip = new ZipArchive();
if ($zip->open($file) !== TRUE) {
die ('Could not open archive');
}
$zip->extractTo('../img/media');
$zip->close();
echo 'Archive extracted to directory';
?>
Related
Hello this is my first time posting here, I am not sure how this works but I have included my code below. I would greatly appreciate some help.
I am having trouble with closing my zip file. The markers (print statements) that I have created are being executed. I am only getting an error with $zip->close().
For reference, I have PHP Version 7.4.24 and am I using a Mac. When my colleague ran it on his Windows system, it executed.
This is the error that is displayed in my browser:
Warning: ZipArchive::close(): Failure to create temporary file: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/nameOfMyFolder/folderzip.php on line 55
<?php
// name of directory (folder)
$pathdir = "/Applications/XAMPP/xamppfiles/htdocs/nameOfMyFile/";
//name of zip file to be created when zipped
$zipcreated = "archive.zip";
// new zip class
$zip = new ZipArchive;
if (extension_loaded("zip")){
echo "Zip extension is loaded";
}
//phpinfo();
// PHP Version 7.4.24
// Create a zip file and open it, check if it worked
if($zip -> open($zipcreated, ZipArchive::CREATE ) == TRUE) {
// Store the path into the variable
// opendir opens a directory handle
$dir = opendir($pathdir);
while($file = readdir($dir)) {
// is_file checks if specified file is a regular file
echo $pathdir.$file;
if(is_file($pathdir.$file)) {
$zip -> addFile($pathdir.$file, $file);
echo "File/s copied";
} else {
//echo "File not copied";
}
//echo "While executed";
}
echo "Out of while loop";
$zip->close();
//$zip -> ZipArchive::close();
//zip_close(resource ($zip));
//$zip -> getStatusString();
} else {
die ("Can't open $zipcreated");
}
?>
I've refactored your code and it appears to be working now. Take note of how I used the "! not" condition so the code can exit quicker and it doesn't need to be nested.
<?php
$folder_to_archive = "/path/to/your/folder/";
$zip_file = "archive2.zip";
if (!extension_loaded("zip"))
die("Zip extension could not be loaded" . PHP_EOL);
$zip = new ZipArchive;
if ($zip->open($zip_file, ZipArchive::CREATE) !== true)
die('Could not create Zip File' . PHP_EOL);
$dir = array_diff(scandir($folder_to_archive), ['.','..']);
foreach ($dir as $file) {
$full_filename = $folder_to_archive . $file;
if (!is_file($full_filename))
continue;
$zip->addFile($full_filename, $file);
}
$zip->close();
I need to read files from ZIP archives with PHP.
And my zip is not on the disk. It is accessible with http url.
There is the extension ZipArchive . It works fine if a file is on disk . Is it possible to use this extension with some opened resource instead of opening a file by path?
Important note, i can not download a zip file temporary to disk. I can work only with the file remotely.
If not ZipArchive, maybe there are other similar libraries for Zip that can read from stream?
There is simple example which doesn't work.
$zip = new ZipArchive();
$fh = fopen('zipfiles/1.zip','r');
$status = $zip->open($fh);
if ($status !== TRUE) {
exit("Error $status");
}
echo "Number of files: ".$zip->numFiles;
But this one works fine
$zip = new ZipArchive();
$status = $zip->open('zipfiles/1.zip');
if ($status !== TRUE) {
exit("Error $status");
}
echo "Number of files: ".$zip->numFiles;
I am trying to extract zip file in codeigniter 4... I have the code below
$file_name = session()->get('file_name');
// Zip file name
$filename = "/public/$file_name";
$zip = new \ZipArchive;
$res = $zip->open($filename);
if ($res === true) {
// Unzip path
$path = "/public";
// Extract file
$zip->extractTo($path);
$zip->close();
return 'Site Updated Successfully';
} else {
return "failed to update $filename!";
}
}
but I get this result: return "failed to update $filename!";
Please can someone help me fix the issue with the code.
This has nothing specifically to do with Codeigniter. It's just using PHP and ZipArchive. The code looks OK - be sure the file is really in the /public/ directory and that it is a zip archive. The PHP site has examples that are like $zip = new ZipArchive; (without the \ char); maybe try that.
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?
<?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
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
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();
?>
I'm running a php script (like bash).
This script do this:
Download a zip file from FTP server
ftp_get($ftp, $myFile.zip, $file, FTP_BINARY);
When I have downloaded the file, I want to extract its content:
$zip = new ZipArchive;
$res = $zip->open($myFile);
if ($res === TRUE) {
$zip->extractTo($extractDir);
$zip->close();
} else {
dump( 'error, code:' . $res);
die;
}
But, I get the error 19 who is: "Not a zip archive"
If I open the .zip, it is a compressed archive because I have the PK key...
Does anyone have a solution or idea ?
Regards
Solutions
I am using this code and it work fine.
$zip = new ZipArchive;
$myfileDir = './abc.zip';
$extractDir = './';
$res = $zip->open($myfileDir);
if ($res === TRUE) {
$zip->extractTo($extractDir);
$zip->close();
echo 'Successfully extract the file';
} else {
echo ('error, code:' . $res);
die;
}
Question
I believe the problem is because of the zip file, would upload the the zip folder to google drive and let us download and test it locally?