The user uploads a .tar.gz archive and I need to validate if the archive contains a certain file (I just need to check the filename, nothing else).
I know I can open the file using \PharData however it seems that it loads the entire archive into memory. Since the archive is a backup and can easily be bigger than memory_limit, I can't use this.
How can I do the validation without loading the whole file to memory? In case it is impossible to do in PHP (as I suspect), how can I do it in bash? Again without loading the entire file to memory at once and also without actually unpacking the archive.
you just can use bash command for that:
tar -t - show list of files
Related
So as described in question itself, I want to replace the file from which a zip archive is opened and then which is overwriting files with new version.
If still my question is not clear then the thing I want to do is I want to get a zip file from a server and then unzip using CLASS "ZipArchive" and then over write everyfile which is in Zip to destination location, the problem will be the php file by which this thing is happening will gonna be overwritten.
So will the php generate the error or the process will go whatever we want?
On Linux files are not usually locked (see https://unix.stackexchange.com/questions/147392/what-is-advisory-locking-on-files-that-unix-systems-typically-employs) so you can do whatever you want with that file. PHP works with that file in memory so you can overwrite it during it's execution.
But if you will run the script multiple times while the first one is in progress it might load incomplete version and then it will throw some error so it might be wise to make sure that won't happen (using locks) or try to do some more atomic approach.
Windows locks files so I assume you won't be able to extract files the same way there.
I am working with SFTP server. How can I view the list of files in .tar.gz (5 GB size) file without downloading and extracting it? I am using phpsiclib1.0.7 for that.
I am successfully login on SFTP server and list all the directories. For that I have used below code.
<?php
include('Net/SFTP.php');
$sftp = new Net_SFTP('sftp.tripadvisor.com');
if (!$sftp->login('username', 'password')) {
exit('Login Failed');
} else {
$current_dir= $sftp->nlist();
$sftp->nlist('drop/listings/');
}
?>
Quoting "Limitations > Random access" section of Wikipedia "tar" article:
Another weakness of the tar format compared to other archive formats (like DAR or Zip) is that there is no centralized location for the information about the contents of the file (a "table of contents" of sorts). So to list the names of the files that are in the archive, one must read through the entire archive and look for places where files start.
"read through the entire archive" - That effectively means downloading whole archive, what you do not want to do.
"look for places where files start" - For that you need a random access to the archive file. While the SFTP protocol allows a random access to a file, your tar is actually in a gzip file. And compressed files, at least the gzip format, do not allow random access.
In my opinion, contrary to the wording of the Wikipedia article, you do not need to "read through the entire archive", you can just skip from one file start to another. What is slightly better. But as long as the .tar is in .gz, it does not really help you in your cause.
Well you can use the Php Phar extension to extract the 5Gb tar.gz file. After extracting the file you can read the contents of the extracted folder using scandir function.
To automate the process of extracting and listing files, you can use the SSHPass utility. You can run the SSHPass from Php using exec function. The output of the exec function should show the list of files.
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
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.
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()