adding remote files to a zip file - php

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.

Related

How should the temporary file be saved when creating and download zip file using PHP?

I wish to zip a directory on the server and download it to the user. I've found great information on this site on how to zip the directory.
The part that has me stumped is where to store the temporary file. From what I've read, zipArchive requires that the zip file be saved on the server, and I can't download it without saving it. Correct?
I've seen solutions where they recommend saving it as "somename.zip", and then using readfile() along with the appropriate headers to download, and then using unlink() after the download to get rid of it. But where to store "somename.zip" and what if there are collisions?
So, then I started thinking that I should create a file with a random name in some temporary directory. Or maybe I should use something like tmpfile(), but that returns a file handler and I don't know how that will work with zipArchive.
For my application, the size of the directory is fairly small, and would like to not even write it to disk, but to memory instead. Maybe something like $tmp_handle = fopen('php://temp', 'r+');, but again this returns a file handler and I don't know how that will work with zipArchive.
How should the zip file created by zipArchive be temporary saved?
As ZipArchive class works with stacked streams, the $filename parameter cannot be php://temp or php://memory. It has to be a 'real' filepath on the filesystem.
Some lines of thought :
As said in a comment, tempnam() function can help you to avoid temporary files names collisions. I think storing the file temporarily and readfile()'ing it is the most common and not a bad option
If you absolutely want that temporary file be stored in memory, you can mount a tmpfs directory on your Linux system. It will be seen as a 'normal' directory for PHP but will be stored in RAM
If you can use shell_exec() function, you can zip the directory in a command line and read the output in PHP : it will be stored in memory.
In my opinion, the first option (your first idea) is better in most cases, because :
allowing the use of shell_exec() can be dangerous
if one day you have big files to zip, this could fill the memory, resulting in very poor performance

PHP rename file during ftp download

So a PHP program I built to download files from one server and upload them to another is now needed to rename the files.
For some reason I'm getting a 'invalid argument' warning on the rename() function which seems impossible because the file exists and there is 0 white space.
So now I was wondering if it was possible to rename a file as I download it from an ftp using ftp_get()?
http://php.net/manual/en/function.ftp-get.php
Would suggest so. Give it a local file name.
Also make sure you are using rename() correctly
http://uk.php.net/manual/en/function.rename.php

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.

Combine multiple files and folders into one file using PHP

I have designed an online editor with which a user can create files and folders on the server. Suppose a user creates the below files and folders.
/docs/abc.txt
/docs/def.txt
/docs/work/assignment.txt
I want to write a PHP script to combine the files into a single ZIP file so that it makes it easier for the user to download them with one click.
The only purpose is that I should get a single file with the directory hierarchy maintained which the user can download and uncompress on his system to get the original files and directory structure back. I don't care even if the ZIP file is not compressed.
Note that I am using a shared server and do not have access to execute external commands.
ZipArchive class is what you need. Using this you can add directories and file to an archive.
This is a common requirement and has been solved. Try and check out this class: http://www.phpclasses.org/browse/package/2322.html
More tutorials at:
http://davidwalsh.name/create-zip-php <-- this has a similar example if you scroll down
http://www.w3schools.com/php/php_ref_zip.asp
Assuming you have permission to execute an external command, you could use exec to run an external command-line capable ZIP program such as 7-Zip.

'pass through' php upload to amazon's s3?

I've seen ways to upload files directly to S3 and I've seen ways to upload a file already on the server. I need to modify the file's name before sending it on to S3, but I don't want to store it locally and then upload it, if I can help it. Is there a way to put it in a buffer or something? Thx.
The file will always end up in the temporary directory first while the upload completes, even before you're able to work with the uploaded file. You get all the file's chunks before, and then it get rebuilt in the /tmp directory by default. So no, there's no "pass-through". But I guess you could re-upload directly from the temporary directory afterwards instead of moving it to another working directory.

Categories