Simple solution to extract PDF.zip using PHP - php

I have a rather large zip file (I used 7zip to compress the files) and I must now extract the files that are on the server. I tried using CPANEL File Manager but the zip file is too big.
Any ideas on how I can extract this zip file?
I do not have SHELL ACCESS and would prefer to use a software tool, if there is one out there.

Can you try, maybe this works. You can also use exec function
<?php
$salida = shell_exec('unzip you.zip');
echo $salida;

Related

Extract ZIP in php, overwriting everything

I'm extracting server side .zip using this:
<?php
$unzip = shell_exec("unzip zipp1.zip");
?>
It's working fine, but it doesn't overwrite existing files (and I need it!).
Everything is in the same folder, chmod 777.
Can I add something to fix?
Tnx!
If you really need to use shell, you can write unzip -o zipp1.zip.
However, PHP has a library for working with Zip archives, called ZipArchive: http://php.net/manual/en/class.ziparchive.php
There you can extractTo which overwrites by default: http://php.net/manual/en/ziparchive.extractto.php
It's usually a good security practice to disable shell_exec(), so using PHP's libraries is recommended.

PHP Importing XML feed packed in a zip file

I want to automate the following:
Once a day my cronjob starts a PHP script that obtains a zipped XML file from an URL.
What would be the best way to handle this? Is there any way to directly read the XML file from within the zip file?
Right now, i'm just downloading the zipped file to the server and manually unpacking it later that day.
Any ideas? All suggestions are very much welcome.
You can use PHP's ZipArchive coupled with cURL to download and read the zip file.
Also, the ZipArchive class has a method called getStream which allows you to then use fread to access the contents without explicitly extracting the file.
The only problem I see if that the zip does have to be saved somewhere for PHP's library to read it. But, given you're already doing this, it shouldn't be an issue.
If you need an example, leave me a comment and I can write on up.
There is a collection of zip-related functions that can be used in PHP.
The problem with these is that it requires the compressed file to exist on the server (not just loaded from an external server somewhere using, for example, $file = file($url);).
If you were to save the file to your server then you could use $zip = zip_open($filename) and zip_read($zip) to process the zip file.

Extracting self-extracting exe archive via php

I have a problem. I have the service which is giving me .exe file which they claim is in fact zip archive. A self-extracting archive.
Problem is that I am downloading that with my app (php) to server and need to extract it there witout downloading to local computer.
I have tried download .exe file to local computer - it is self extracting on windows to /temp dir and than self launching FLASH player.
$zip = zip_open($myfile); gives in print_r($zip): 1
zip->open gives no results either.
change .exe to .zip doesn't let winzip or other kind of un-packer on windows to open it - .exe cannot be opened by winzip too.
Now I have no idea how to deal with it. If anybody can advise please.
Try to execute the program as an executable with the system command
Executing files from an external source you don't trust 100% is never a good idea.
The info-zip version of zip allows you to remove the SFX stub from a self-extracting zip file (with the -J flag) converting it back into a normal zip file.
Source code is freely available.
Making a self-extracting zip file is a matter of prepending a zip file with the SFX binary code, then appending the size of the binary stub to the resulting file - but I'm not sure how the data is represented - but a bit of reverse-engineering the available code should make this clear.
Well... if your PHP server is Windows you shouldn't have a problem doing it as a system command. Otherwise, it's a little more tricky. I hear that the unzip system command will unzip self-extracting zip files, but I don't have access to a Linux box at the moment to try it out.
If you're on shared hosting, chances are you can't do it.
Well if you think after executing the exe file, it will extract its content, then you can use exec function to run the .exe files like the one below:
exec("d:\\example\\php\_exe\\1436.exe");
and also you can use system function to run external programs as well.
And also if you wonder what's the difference:
PHP - exec() vs system() vs passthru()

adding remote files to a zip file

Is there a way to add files to a zip file from another server with php's zip extension? ie.
addFile(array('localfile.txt,'http://www.domain.com/remotefile.txt'))
//(that obviously does not work)
I suppose I can download the files to a temporal folder and then add them to the zip file, but I was looking for a more automated solution or a function already made
use file_get_contents() and ZipArchive::addFromString()
$zipArchiveInstance->addFromString($filename, file_get_contents($mediaUrl));
This writes the contents fetched remotely straight into your php object (no need to write/read temp file)
It's not hard to read remote files in PHP.
file_get_contents("http://example.com/remote.txt");
Or to copy them locally:
copy("http://example.com/remote.txt", "/tmp/local.txt");
Whichever way you do it, the contents are going to have to be transferred to a local temp folder or memory before you can do anything with them.
Fetch them with cURL, add them from TEMP directory.

When using system to zip a directory lots of output is shown

I'm trying to write a script that allows an admin of a photo uploading system download all their photos at once.
Currently I am using
system('zip -r '.$_SERVER['DOCUMENT_ROOT'].'/zip.zip '.$_SERVER['DOCUMENT_ROOT'].'/images/photo-uploads';
to zip the files but this seems to echo names and locations all the files onto the page.
Is there anyway to get around this? If not what is the best way to zip files on server.
You might use exec('zip -r '.$_SERVER['DOCUMENT_ROOT'].'/zip.zip '.$_SERVER['DOCUMENT_ROOT'].'/images/photo-uploads'); instead.
You can use ZipArchive extension instead (if you are allowed to) of calling system zip like that, because it makes your code non-portable.
You can also use output buffering:
ob_start();
system('zip -r '.$_SERVER['DOCUMENT_ROOT'].'/zip.zip '.$_SERVER['DOCUMENT_ROOT'].'/images/photo-uploads';
ob_end_clean();
This will stop any output from being shown from the system command.

Categories