PHP Importing XML feed packed in a zip file - php

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.

Related

How can display text content itself using PHP from Azure File with Azure storage account?

I have been installed the git lib from https://github.com/Azure/azure-storage-php .
I able to figure out the create file, download file, delete file.
But how can get the file content of the file and display on browser ?
Thank you.
I'm not that much knowledgeable about PHP but I believe the SDK method you would want to use is getBlob which reads a blob and returns an object of type GetBlobResult.
Contents of the blob can be read using getContentStream() method there.
If you're using Azure Files, more or less the approach would remain the same. You would be calling getFile method which reads a file and returns an object of type GetFileResult.
Contents of the file can be read using getContentStream() method there.

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

Simple solution to extract PDF.zip using 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;

Value too large for defined data type

I'm using readfile() in a php download script.
When I try to download a 9gb sized file, I get the following error:
function.readfile</a>]: failed to open stream: Value too large for defined data type in path of my file
Is it possible to fix it or do I have to move those files to the public_html directory and link them directly?
you can hack a way though. If you are on a unix environ, you can do
passthru('cat $filename');
as long as you don't need to write
You most likely need to re-compile php with CFLAGS="-D_FILE_OFFSET_BITS=64" so that PHP is able to handle large files. There's comment about that on the fopen documentation page.
Some additional reading:
http://en.wikipedia.org/wiki/Large_file_support
Althought you might not be using SUSE, still some interesting information: http://www.suse.de/~aj/linux_lfs.html

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.

Categories