Unzip latest zip file with php - php

Can someone help me to unzip the newest zip file in a directory.
I can unzip a file using the specific file name, but unfortunately, the file name changes daily. So unzipping the latest file is my best option.
Or
Can someone show me how to unzip a zip file using the *.zip wildcard and then delete the zip file.
I have tried using the wildcard to unzip, but the wildcard doesn't work for the initial unzip.
$zip = new ZipArchive;
$res = $zip->open('/home/*.zip');
if ($res === TRUE) {
$zip->extractTo('/home/');
$zip->close();
echo 'woot!';
} else {
echo 'doh!';
}

Related

execute php page that unzip a file (manipulate files)

Im using a local (rhel 8) server to unzip a file using php (solution proposed in : https://stackoverflow.com/a/8889126/9583635)
<?php
// assuming file.zip is in the same directory as the executing script.
$file = 'temperatures.zip';
// get the absolute path to $file
$path = pathinfo(realpath($file), PATHINFO_DIRNAME);
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($path);
$zip->close();
echo "Success! $file extracted to $path";
} else {
echo "Failled! I couldn't open $file";
}
?>
The problem is:
when I execute this code on the server using:
php /var/www/html/unzip.php
it works fine (success message) and I find the extracted file in the same directory.
but when I use browser (http://192.168.1.5/unzip.php) the success message is printed but I can't find the unziped file in this directory.
note: I tried using wamp server and it works, so the problem may be in the server.
By the way all the php line codes that were manipulating files wasen't taking affect. I solved this by allowing the Apache user (apache in rhel) to create files in the directory. This can be done by making Apache the owner of the directory running this cmd:
sudo chown apache /var/www/html/

override all files and folders while unziping except given folder name using php

My file structure in zip is
lib
css
js
app1
app2
index.php
All the above files and folders are in test.zip
i want to extract this test.zip using php
$zip = new ZipArchive;
$res = $zip->open("test.zip");
if ($res === TRUE)
{
$zip->extractTo('path');
$zip->close();
}
if lib folder is already exist in the directory where we are unzipping then except lib(folder) all other files and folders should get override.
how to solve this.
http://php.net/manual/en/ziparchive.open.php , on open, pass in the over write flag.

Corrupt ZIP files using PHP & PCLZip

I'm having some issues, I'm using PCLZip to create an archive. I don't get any errors, the zip file is created, but when I go to view it, the archive is empty and on my windows machine I get an error "The Compressed (zipped) folder "local directory zip file") is invalid. I have the following code:
$dir = '../downloads/liability/';
$archive = new PclZip($dir.'archive.zip');
$v_list = $archive->create($dir);
if ($v_list == 0) {
die("Error : ".$archive->errorInfo(true));
}
My directory structure is:
-admin
--liabilityDev.php (where the above code resides)
--index.php
--commission.php
-downloads
--liability
---one.pdf
---two.pdf
The end result is that in the liability folder, there is a file called archive.zip which contains the 2 pdf's but I get the invalid error.
If I don't have the directory variable, I archive index.php and commission.php and that works fine. It leads me to believe it may be a permission issue, but I'm running on fumes now. Please help!
You can try this:
if(extension_loaded('zip')){
$zip = new ZipArchive();
if($zip->open('../downloads/liability/archive.zip', ZIPARCHIVE::CREATE)===TRUE){
$zip->addFile('path of any normal file to be add into zip');
}
$zip->close();
}
I think, this will fullfill your need. Before implementing this code, please check first that, zip extension is already loaded or not.

Upload a .zip file to FTP and unzip it using PHP

I'm trying to make a script that would upload a certain zip file , currently "test.zip", and then unzip it. I found some answers in this website about how to unzip but got no idea how to do it on an FTP server. All using PHP Please.
Current code:
$zip = new ZipArchive;
$zip->open('test.zip');
$zip->extractTo('./');
$zip->close();
Thanks again :)
Before unzipping it you can upload the zip by using a form (make your own admin area?/localhost only?). I copied this from my code for uploading images and will only upload .zip files
HTML part:
<form action='file.php' enctype='multipart/form-data' method='post'>
<p>Please Upload a ZIP File.</p>
<input type='file' name='file'>
<input type='submit'/>
</form>
PHP part:
$temp = explode(".", $_FILES["file"]["name"])[1]; //Get the extension
if($temp == "zip"){
move_uploaded_file($_FILES["file"]["tmp_name"],"unzipme.zip") or die("Couldn't upload. Check permissions and retry.");
//Relocate home?
header("Location: /");
exit();
}
You need permissions to upload to your directories, www-date or something similar.
Hope this helps.
First you should upload the zip archive to the server.
Here is an example of how to do basic FTP things in PHP.
http://www.php.net/manual/en/ftp.examples-basic.php
After that you should be able to unzip the file on the server using the method you described in the question.
You either need access to execute commands or programs on the machine you are uploading to, or the ability to also upload a script that you can execute via a URL. If you are uploading via FTP to a web server directory that can run PHP, then upload your zip archive and the unzip.php file there and load the unzip.php page in your browser.
This is the code for Extract folder in your FTP, create a new Php file copy below code and run it:
$path = getcwd();
$zip = new ZipArchive;
$res = $zip->open('yourZippedFolder.zip');
if ($res === TRUE) {
$zip->extractTo($path.'/maunil/');
$zip->close();
echo 'Successfully Extracted';
}
else
{
echo 'failed to Extract';
}

Extract the zip file using php script

I have a zip file and i used the following code to extract that zip and put all extracted file in another location.
$zip = new ZipArchive;
echo $zip;
if ($zip->open("$pwd/wordpress-3.4.2.zip") === TRUE) {
$zip->extractTo("$pwd/Repo/");
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
But i cant any extracted file whether it show 'ok' in browser. What is the mistake in the code i cant found. another thing to download the zip file from the "http://wordpress.org/latest.zip" site. i used the code written bellow. Here also i cant download the file.
$foo = system('wget http://www.myserver.com/file.txt ~',$output);
You could also use:
exec('wget http://wordpress.org/latest.zip -O temp.zip');
exec('unzip temp.zip -d /somedir');
It appears that the ZipArchive needs a reference to the library containing that class.
Also make sure that in your php.ini file, you have enabled the library that enables zip files functions to work on your server. (The zip extension is loaded by default under PHP 5.3)
Again, make sure the directory you want to extract the files has write permissions.

Categories