Can someone post code or point to a working example of a php script that opens a known zip file located on a remote server (via http) and extracts the contents of that zip file to a folder on the same server as the calling script?
Server A calls out for zip file located on Server B
Server B sends the zip file over to server A
Server A extracts the contents of the Zip to Directory C (pub_html) on Server A
I've been going around and around on how to get this to work and it really seems like it should be super simple with lots of examples, but I can find none.
Here you go.
Alter $filename and $dest_folder at the top accordingly. $dest_folder is currently set to current folder (where the PHP script itself resides).
<?php
$filename = 'http://someplace.com/somezipfile.zip';
$dest_folder = '.';
$out_file = fopen(basename($filename), 'w');
$in_file = fopen($filename, 'r');
while ($chunk = fgets($in_file)) {
fputs($out_file, $chunk);
}
fclose($in_file);
fclose($out_file);
$zip = new ZipArchive();
$result = $zip->open(basename($filename));
if ($result) {
$zip->extractTo($dest_folder);
$zip->close();
}
?>
The documentation demonstrates a simple case:
<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
echo 'ok';
$zip->extractTo('test');
$zip->close();
} else {
echo 'failed, code:' . $res;
}
?>
You'll want to be mindful of script-execution time, depending on how large your remote archive is, and how quickly your server can communicate with the remote server.
Ok first i suggest you write a curl function or class which fetsches for you the zip file and stores them to a temp folder or somewhere else. After Saving the zip file i assume you know the name of it. Then you could simple do this
<?php
$zip = new ZipArchive;
$zip->open('test.zip');
$zip->extractTo('./');
$zip->close();
?>
Although this is simple for extraction a zip file. It needs some error cathing but it does the job Have Fun :-)