Help with php function zip_open - php

It's now 5am, and as much as I try to research, I can't find much information on this function set. Here's the code I have (shortened slightly):
<?php
$source = $_FILES["restore_file"]["tmp_name"];
$zip = zip_open($source);
while ($zip_entry = zip_read($zip)) {
echo zip_entry_name($zip_entry).'\n';
}
?>
which when I upload my example zip out puts:
example/
example/index.php
example/file1.csv
example/file2.csv
example/file3.csv
etc.
I need to know how to access the contents of those files though, and also be able to specify which file I am accessing exactly. For example, before going through the csv files, I need to check a php variable in the index.php file of the archive, to make sure it is correct.
Is using the ZipArchive class a better idea instead of the zip functions perhaps? I was under the impression though that using the zip functions would be better as it can access the files on the fly (without having to transfer the files to a new directory).

Use ZipArchive::getFromName(). Example from the PHP manual adapted to your case:
$zip = new ZipArchive();
if ($zip->open($_FILES["restore_file"]["tmp_name"]) === true) {
echo $zip->getFromName('example/index.php');
$zip->close();
} else {
echo 'failed';
}

Related

In PHP - Overwrite Specific File in Zip Folder [duplicate]

I have a ZIP file on my server. I want to create a PHP file, loadZIP.php that will accept a single parameter, and then modify a text file within the ZIP to reflect that parameter.
So, accessing loadZIP.php?param=blue, will open up the zip file, and replace some text in a text file I specify with 'blue', and allow the user to download this edited zip file.
I've looked over all of the PHP ZIP functions, but I can't find a simple solution. It seems like a relatively easy problem, and I believe I'm over thinking it. Before I go and write some overly complex functions, I was wondering how you'd go about this.
Have you taken a look at PHP5's ZipArchive functions?
Basically, you can use ZipArchive::Open() to open the zip, then ZipArchive::getFromName() to read the file into memory. Then, modify it, use ZipArchive::deleteName() to remove the old file, use ZipArchive::AddFromString() to write the new contents back to the zip, and ZipArchive::close():
$zip = new ZipArchive;
$fileToModify = 'myfile.txt';
if ($zip->open('test1.zip') === TRUE) {
//Read contents into memory
$oldContents = $zip->getFromName($fileToModify);
//Modify contents:
$newContents = str_replace('key', $_GET['param'], $oldContents)
//Delete the old...
$zip->deleteName($fileToModify)
//Write the new...
$zip->addFromString($fileToModify, $newContents);
//And write back to the filesystem.
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
Note ZipArchive was introduced in PHP 5.2.0 (but, ZipArchive is also available as a PECL package).
In PHP 8 you can use ZipArchive::replaceFile
As demonstrated by this example from the docs:
<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->replaceFile('/path/to/index.txt', 1);
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>

Problems when extract ZIP file in PHP

I'm trying to extract zip file to some folder in storage_path (I'm using laravel). But I don't know why they comes with .(dot)files when it extracted, also I want to extract them without creating another folder. Here's the code I'm using:
lines inside some public function:
$src = storage_path('application');
$dst = storage_path('tmp/'.time());
$zipfile = $request->file('splashicon');
$zipfile->move($dst, 'splashicon');
$this->splashIcon($dst.'/splashicon', $dst);
splashIcon function:
public function splashIcon($src, $dst)
{
$zip = new ZipArchive();
$x = $zip->open($src); // open the zip file to extract
if ($x === true) {
$zip->extractTo($dst.'/www'); // place in the directory with same name
$zip->close();
unlink($src); //Deleting the Zipped file
}
}
The current result is:
1494618313/www/name.files/thecontentsofzipfile
I have no idea where .(dot) and files came from, please explain what was happened to them.
And by the way, the correct result should be something like this:
1494618313/www/thecontentsofzipfile
Thank you,
This is a laravel convention.
The . means it's a subfolder/subfile within your directory.
I'm not really sure how to get around it.
You might look into Zipper, which is an extraction helper for Laravel. I believe it has an exact match for extraction instead of using the typical Laravel syntax.
https://github.com/Chumper/Zipper

Reading files from ZIP without extracting to disk

Is it possible to open a ZIP file on a server, read a file from its content and display it / send it to a client directly without extracting it to disk first ? I am talking about pdf's and images. Haven't found any hints in the php sites.
Well,there is a PHP Extension.
If you use the extractTo method, you would be able to extract a single file, checkout the documentation.
From the documentation, extracting two files:-
<?php
$zip = new ZipArchive;
$res = $zip->open('test_im.zip');
if ($res === TRUE) {
$zip->extractTo('/my/destination/dir/', array('pear_item.gif', 'testfromfile.php'));
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
You would need to provide an array of path inside the zip.

overwrite zip file using php

I wrote a method to create a zip entry and rewrite it if this function is called second time but it is not working. here is my code:
public function zipFile($filepath,$fileName){
$zip = new ZipArchive;
$zip_name = $fileName.'.zip';
echo "$zip_name";
if($zip->open($zip_name, ZIPARCHIVE::OVERWRITE)===TRUE) {
$zip->addFile($filepath,$fileName.'.csv');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
return '/home/daily_reports/'.$zip_name;
}
what is missing in my logic. I want to replace the file with new one if the method is called again
Perhaps try explicitly deleting the zip file first if it exists. The overwrite option may not behave as expected.
clearstatcache();//For good measure clear out any cached file paths
$file = "{$filepath}/{$fileName}"
if(file_exists($file)){
unlink($file);
}
However, I have had mysterious issues trying to use the built-in zip functionality in php, especially with platform differences, performance, and memory issues. I prefer to zip the files on the command line through php.
On linux/osx:
$cmd = "zip archivefile file1 file2";
exec($cmd);
On windows use 7zip, also from the command line. http://www.dotnetperls.com/7-zip-examples
$cmd = "7za.exe a archivefile.7z file1 file2";
exec($cmd);
Technically you don't need to install 7zip, you just need the stand alone exe, but you might need to install it first to get the exe. http://www.7-zip.org/download.html

Modifying a single text file in a ZIP file, in PHP

I have a ZIP file on my server. I want to create a PHP file, loadZIP.php that will accept a single parameter, and then modify a text file within the ZIP to reflect that parameter.
So, accessing loadZIP.php?param=blue, will open up the zip file, and replace some text in a text file I specify with 'blue', and allow the user to download this edited zip file.
I've looked over all of the PHP ZIP functions, but I can't find a simple solution. It seems like a relatively easy problem, and I believe I'm over thinking it. Before I go and write some overly complex functions, I was wondering how you'd go about this.
Have you taken a look at PHP5's ZipArchive functions?
Basically, you can use ZipArchive::Open() to open the zip, then ZipArchive::getFromName() to read the file into memory. Then, modify it, use ZipArchive::deleteName() to remove the old file, use ZipArchive::AddFromString() to write the new contents back to the zip, and ZipArchive::close():
$zip = new ZipArchive;
$fileToModify = 'myfile.txt';
if ($zip->open('test1.zip') === TRUE) {
//Read contents into memory
$oldContents = $zip->getFromName($fileToModify);
//Modify contents:
$newContents = str_replace('key', $_GET['param'], $oldContents)
//Delete the old...
$zip->deleteName($fileToModify)
//Write the new...
$zip->addFromString($fileToModify, $newContents);
//And write back to the filesystem.
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
Note ZipArchive was introduced in PHP 5.2.0 (but, ZipArchive is also available as a PECL package).
In PHP 8 you can use ZipArchive::replaceFile
As demonstrated by this example from the docs:
<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->replaceFile('/path/to/index.txt', 1);
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>

Categories