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/
Related
After moving a website from one server to another, both running IIS, when a zip file creation script is called the file created has some random characters at the end, ex. what should be 'test.zip' is converted to 'test.zip08c1b35d' giving error when trying to download them.
I've discovered that it only happens inside the website path, if I create a symbolic link inside the website path with the same name, pointing to a folder outside the website path the zip file is created with the correct name. Any other folder within the website path the name is changed.
The zip file format is correct as if I delete the characters and leave the name ending '.zip' the file opens fine.
The code used is:
$dir ='../mylims/test/';
$destination = $dir.'test.zip';
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
echo "Error";
}
$zip->addFile('./test/testfile.txt','testfile.txt');
$zip->close();
Any suggestions what to look? Tks
I write a program to unzip a file using ZipArchive's extractTo() function only. It runs ok on Windows wherease in Linux, it treats folder name as part of file name
Users give me two zip files: onepiece.zip and file.zip. They both have structures with several top-level folders and in each folder, there're only one jpg and one txt file.
When I run my program to unzip onepiece.zip, it works fine both under Windows and Linux.
However, if I run my program with file.zip, it still works fine under Windows but it will unzip strange result under Linux as follows:
20200618212731318\20200618212731318-2-1afa34bb-0cf8-450c-aeb6-e0bf2b95d5dc-Source.jpg
20200618212731318\20200618212731318-2-1afa34bb-0cf8-450c-aeb6-e0bf2b95d5dc-Source.txt
20200618202449404\20200618202449404-2-d3d15882-0e3b-4c07-b614-af8dd649246c-Source.jpg
20200618202449404\20200618202449404-2-d3d15882-0e3b-4c07-b614-af8dd649246c-Source.txt
20200618202436965\20200618202436965-2-1d5c128a-a145-406b-95d5-66aee0b1eb11-Source.jpg
20200618202436965\20200618202436965-2-1d5c128a-a145-406b-95d5-66aee0b1eb11-Source.txt
20200618202453983\20200618202453983-2-cc16f9e9-fa95-408f-8579-b9455c097b84-Source.jpg
20200618202453983\20200618202453983-2-cc16f9e9-fa95-408f-8579-b9455c097b84-Source.txt
20200618202506672\20200618202506672-2-78b44d00-65c3-4030-aa35-fe13b9b225df-Source.jpg
20200618202506672\20200618202506672-2-78b44d00-65c3-4030-aa35-fe13b9b225df-Source.txt
20200618204333943\20200618204333943-2-d9426906-ed40-4828-b6a8-bda202a10fa0-Source.jpg
20200618204333943\20200618204333943-2-d9426906-ed40-4828-b6a8-bda202a10fa0-Source.txt
As you can see, the folder name becomes part of filename. I doubt the problem comes from the zipping tool that Users use. Is there any solution or workaround to solve this problem?
Note: On Windows, I'm using php 5.5.30. On Linux, it's php 5.5.9.
This is the codes of unzipping file. It's quite simple without any particular codes or functions:
// Unzip uploaded zip file
$zip = new ZipArchive;
$res = $zip->open($old_zipfile_path);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($old_unzip_dir);
$zip->close();
echo "<font color=brown>'$old_zipfile_fullname' extracted to $old_unzip_dir</font><br>";
} else {
echo "<font color=red>OOPS!! I couldn't open $old_zipfile_path</font><br>";
}
echo '<br>';
So some reason server changes random (?) .php files to .ph.
Need to rename any .ph file to .php
Tried about every rename and rename extension code I found on stackflow.
Nothing has worked so far.
No root access (shared)
working directory will be /
You can try below code :(Tested in localhost)
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
unlink('test.ph');
?>
If you are looking to rename all the .ph files in the current directory to .php, you can try this:
<?php
$old_extension = '.ph';
$new_extension = '.php';
$files = glob("*$old_extension");
foreach($files as $file){
$filename = pathinfo($file,PATHINFO_FILENAME);
rename($file,$filename . $new_extension);
}
?>
A word of caution: the script will rename the existing files in the current directory without warning.
I highly advise running the above script in a development environment or within a Test directory beforehand so you know what to expect.
Best-
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.
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.