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;
Related
I am working on zip file creation and downloading using PHP (Laravel).
Zip created and download works well. But the problem is in lhaplus software. When extracting the zip file using lhaplus, it has been changing the file name. But in another extractor like 7zip, WinRAR works well. In lhaplus software shows "03510063220_00016501tñ+sÉìtñ+sÉì_spt.pdf" but it should be 03510063220_00016501社名社名_spt.pdf.
Please help in this case. Any suggestion is appreciated.
The code is given below:
$date_time = static::getCurrentTime();
$zip_name = $date_time."_spt";
$zip = new ZipArchive;
if(!file_exists('zip/downloadedZip')){
mkdir('zip/downloadedZip',0777,true);
}
$zipFileName = 'zip/downloadedZip/'.$zip_name.'.zip';
if ($zip->open(public_path($zipFileName), ZipArchive::CREATE) === TRUE){
$fileName = "pdf/03510063220_00016501社名社名_spt.pdf";
$baseName = "03510063220_00016501社名社名_spt.pdf";
if (!$zip->addFile($fileName, mb_convert_encoding($baseName, "SJIS", "UTF-8"))) {
throw new \RuntimeException(sprintf('Add file failed: %s', $fileName));
}
$zip->close();
}
return URL($zipFileName);
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();
?>
Imagine there is a picture at http://example.com/icon.jpg and I want to add it to a zip file on my own sever named "Stack.zip" using php. This is my code, but it doesn't work.
$url="http://example.com/icon.jpg"
$zip = new ZipArchive;
echo $zip->open("Stack.zip");
$zip->addFile($url);
$zip->close();
P.S. I was able to do it with local files, but I had no success on doing it with internet addresses. So that's why I asked this question.
From the PHP manual: http://php.net/manual/en/ziparchive.addfile.php
(PHP 5 >= 5.2.0, PHP 7, PECL zip >= 1.1.0)
This assumes you want to add to an existing zip file on your server.
$url = 'https://www.stackoverflowbusiness.com/hubfs/logo-so-color.png?t=1499443352566';
$local_path = '/your/local/folder/';
$img = 'icon.jpg';
file_put_contents($local_path.$img, file_get_contents($url));
$zip = new ZipArchive;
if ($zip->open($local_path.'Stack.zip') === TRUE) {
$zip->addFile($local_path.$img, $img);
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
If you want to keep the same folder structure as the original source file, change this line ..
$zip->addFile($local_path.$img, $img);
...to this...
$zip->addFile($local_path.$img);
If you want the same script to create the zip, you can find a PHP function here: Add files to the zip
I'm completely at a loss for explaining why this isn't working. HELP!
$archive = "x.zip";
$zip = new ZipArchive();
$res = $zip->open($archive);
if ($res === 'TRUE') {
$unzip_success= $zip->extractTo('/temp/', "inscriptions.txt")
$zip->close();
}
the target dir "temp" is "0777" permissions
the code obtained from $res is "11" and not "TRUE" as required by the documentation on PHP.net
note: must put the full url for $archive and the first argument of extractTo
if nothing works then check if your server is linux.
if its linux you can run unzip command to unzip your file via php's system/exec function.
i.e
system("unzip archive.zip");
to extract specific file you can check man docs for unzip. many times due to server parameters zip library doesn't work as expected in that cases i switch back to linux commands.
The problem is that you are quoting TRUE, which is a keyword and should be left without single quotes. Plus, you could check if the file exists in the zip archive prior to its extraction with locateName:
$archive = "x.zip";
$zip = new ZipArchive();
$res = $zip->open($archive);
if ($res === true && $zip->locateName('inscriptions.txt') !== false) {
$unzip_success= $zip->extractTo('/tmp/', "inscriptions.txt");
$zip->close();
}
ZipArcive::extractTo is case-sensitive. If file's name to be extracted do not meet zipped one exactly, the method returns false.
I faced the same problem, I have fixed this :)
Use $_SERVER['DOCUMENT_ROOT'] for url.
My code (codeigniter):
$this->load->library('unzip');
$file = $this->input->GET('file');
$this->unzip->extract($_SERVER['DOCUMENT_ROOT'].'/TRAS/application/uploads/' . $file,$_SERVER['DOCUMENT_ROOT'].'/TRAS/application/views/templates/' . $file);
If $res is equal to 11, that means that ZipArchive can't open the specified file.
To test this:
$archive = "x.zip";
$zip = new ZipArchive();
$res = $zip->open($archive);
if($res == ZipArchive::ER_OPEN){
echo "Unable to open $archive\n";
}
Adding document root is what worked for me as well. here is my code
$zip = new ZipArchive;
if ($zip->open($_SERVER['DOCUMENT_ROOT'].'/'.$folder.$file_path) === TRUE) {
$zip->extractTo($_SERVER['DOCUMENT_ROOT'].'/$folder');
$zip->close();
echo 'ok';
}
I meet same problem, but I can open the zip file, it returns true after open.
My issue is I got false after $zip->extractTo().
I finally success after delete files named in CHINESE (NO-ENGILISH) in the zip file.
I had the same problem on Windows 10. The only solution I found was just to try extractTo twice, even though the open() was successful:
$zip = new ZipArchive;
if ($open === true) {
$result = $zip->extractTo($destination);
if ($result === false) {
$result = $zip->extractTo($destination);
}
$zip->close();
}
The fact that the second extractTo() works (with no intervening action) would seem to indicate that there's nothing wrong with the archive or the destination directory.
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';
?>