PHP Unzip very large file - php

I have a zip file on the server. It's 1.1gb made up of thousands of small files. I do not have shell or root access to the server and can only use ftp and create php files.. so far I have tried exec and shell exec but none worked. The server is running free bsd. How can I unzip the file into the directory it is in?

For a pure PHP solution, try PclZip - this would not require you to install any PHP extensions or require shell access - you just need to write access to wherever you want to extract the files.

$filename = '/media/file.gz';
$unzipped_content = '';
$zd = gzopen($filename, "r");
while ($zip_file = gzread($zd, 10000000)){
$unzipped_content.= $zip_file;
}
gzclose($zd);
echo $unzipped_content;

Thanks for the suggestions everyone. I ended up modifying the code in this question to unzip the files.
Unzip a file with php

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.

How can i edit a txt file using php and ftp

I want to edit (write some data) to a txt file located on an ftp server using php.
I looked to this post writing to a file using php ftp
but in this case CHMOD 0777 is applied which is not understood by my server.
As simple as that!
<?php
$handle = fopen("ftp://user:password#example.com/somefile.txt", "w");
?>
It worked!

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;

PHP - read zip file contents without saving on server

I have a remote ZIP file, and I need to read the content of a file located in the zip file, without having to copy the zip file to our server. There should be some solution to solve this, but I can't find it.
Example:
URL - http://example.com/feed.zip and
File in the ZIP Archive is feed.xml
I need to read the content of feed.xml and save it in one variable. Thats it.
I have try with $z = new ZipArchive(); and open, but open can only read files from the server and not remote files.
A super lazy solution would be:
$feed = `wget http://example.com/feed.zip -qO- | unzip - -p feed.xml`;
Note that the url should be escaped (escapeshellarg) if it's not a fixed string.
But you could also investigate other ZIP classes than the built-in ZipArchive. PEAR or PclZip might allow to read from streams without workarounds. (But php://memory or a string stream wrapper might be feasible as well, if you're really bent on eschewing temporary files.)
I had the same problem and the best I could get was saving zip content to temp file and using ZipArchive to get content of zipped file:
public static function unzip($zipData) {
// save content into temp file
$tempFile = tempnam(sys_get_temp_dir(), 'feed');
file_put_contents($tempFile, $zipData);
// unzip content of first file from archive
$zip = new ZipArchive();
$zip->open($tempFile);
$data = $zip->getFromIndex(0);
// cleanup temp file
$zip->close();
unlink($tempFile);
return $data;
}
This is basically implementation of #mauris suggestion.
That requires the concept of temporary files. Technically speaking: yes, you are going to make a temporary copy of the remote zip file that is locally available because your ZipArchive does not work on remote zip files at all.
Following these steps to implement temporary copy should help you:
Make a copy of the remote zip file to a temporary folder.
Use ZipArchive to parse the local copy of the zip file.
Read information in your feed.xml file.
Close and everything and delete the local copy of the zip file.

compressing a folder into different formats in php

hey guys , im dealing with a script that it should compress and folder and make it ready in 3 versions :
7z
zip
tar.gz
surely i did found a way on how to make a zip file out of a folder in php by using a zip compression class
http://www.phpclasses.org/package/2322-PHP-Create-ZIP-file-archives-and-serve-for-download.html
but for 7z and tar.gz format no idea on how to do the compression
is there any way to make an 7z or tar.gz compressed file !?
specially 7z format
I suggest you install the relevant programs on the server, and call them from php via exec() to compress your files for you. Why bother rewriting the compression code when it's already been written?
Assuming Linux and having these (basic) tools already available:
exec("zip -r archive_name.zip directory_to_compress");
exec("tar -zcvf archive_name.tar.gz directory_to_compress");
For 7zip, you can check out this project on SourceForge.

Categories