php unzip treat folder name as file name on Linux - php

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>';

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/

Compressing and extracting a Directory with all its files using PHP

How to compress or zip a directory and then extract alongside with its files and and sub directories using php script.
i have a directory which is needed to be compressed,then transferred to another server and then extracted into a new directory with new name given to it.
PATH.'/'.$TO.'/'.$DIR_WITH_DIR_NAME
First of all if you want to perform zipping and unzipping operations using php it is must to install php zip extension,as i am using Ubuntu as my OS and php7.0 on my server i installed it using this command.
sudo apt-get install php7.0-zip
For zipping parent directory and other sub directories along side with its files you can use this method given in approved answer.i have used it myself and its best to do so.
Transfer file using php cURL to other server.Now to unzip a folder in a new folder whichever you are trying to extract files in,first create that directory using this php code.
#mkdir(PATH.'/'.$TO.$NEW.'/'.$DIR_WITH_DIR_NAME,0777,true);
This will create the directory and give it all read/write permissions
now use this code to extract a directory
$zip = new ZipArchive;
if ($zip->open(PATH.'/'.$TO.$COMPRESSED.'/'.$FILE.'.zip') === TRUE) {
if(is_dir(PATH.'/'.$TO.$NEW.'/'.$DIR_WITH_DIR_NAME)){
$zip->extractTo(PATH.'/'.$TO.$NEW.'/'.$DIR_WITH_DIR_NAME);
}
$zip->close();
echo 'extracted';
} else {
echo 'error in file extracting';
}
That's it all of the files will be extracted into newly created directory enjoy happy coding.

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.

PHP UnRar.exe into a specific directory

Ive just found this script and it is exactly what im looking for, However how can i tell it to extract the files into a specific folder?
Here is the script
<?php
$winRAR = '"C:\Program Files\WinRAR\UnRAR.exe"';
$file="test.rar";
$do ="$winRAR e $file";
exec("$winRAR /?");
print_r($aOut);
exec($do,$aOut);
print_r($aOut);
?>
Im going to be extracting multiple files so i would like it to extract each archive into a a folder with the same name as the archive. So if the rar was called "Test" i want it to extract to a folder called /test/ and extract the files there?
Many Thanks in advance!
I suggest you to use the following command:
$do ="$winRAR x -ad $file $destinationPath";
Where $destinationPath is a destination path.
The "x" command will keep directory structure stored in archive in contrast with "e" command, which extracts files without subdirectories.
The "-ad" command line switch tells unrar to extract archive foo.rar to the $destinationPath/foo directory.
Command line commands and switches are listed in WinRAR help file, in section "Command line mode".
I think what you are trying to do may be easier using PHP's RAR extension. http://www.php.net/manual/en/book.rar.php

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