tags:

views:

51

answers:

3

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.

A: 

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.

Jonathan Sampson
Thanks Jonathan. I need the open('test.zip') to point to a remote directory. open('http:// server.com/update.zip'). Assuming that works, where do I specify the destination directory for the unzipped file? Is it "/test"?
Scott B
@Scott: Yes, you could use `/test` which would place it immediately within your root directory for that account if I'm not mistaken.
Jonathan Sampson
A: 

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 :-)

streetparade
A: 

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();
}

?>
Helgi Hrafn Gunnarsson
Sweet! I'll give it a go.
Scott B
Helgi! Works flawlessly. Thanks!!! Been crawling on this for 2 days! When I change dest_folder the zip still gets written to the script folder. Assuming I want to delete it, once we're done with it. how?
Scott B
For that you would use the unlink() command. A lot of people don't find it immediately because the name isn't what most people would expect. ;) Similar to rename() which is also used to move files, not only to rename them (because it's the same file system operation).In the end, do: unlink(basename($filename));
Helgi Hrafn Gunnarsson
As for the $dest_folder problem, the first thing that comes to mind is that you're using back-slashes in your path. Back-slashes in PHP also happen to be escape characters so you'll have to double them, do \\ instead of \.However, you should really stick to forward-slashes for paths, even if you're hosting on Windows (Windows has back-slashes as folder separators while civilized operating systems use forward-slashes).So instead of for example C:\whatever\out\there, either make it C:\\whatever\\out\\there, or C:/whatever/out/thereHope it helps!
Helgi Hrafn Gunnarsson
@helgi, your script works flawlessly, I was commenting about Jonathan's script earlier. Your's is exactly what I was looking for. I can even use a GET parameter to specify the updater file ($filename), which makes it dynamic. There are no probs with the $dest folder.
Scott B
@Scott B: To write paths you could also use the constant `DIRECTORY_SEPARATOR`, as in `$path = "folder".DIRECTORY_SEPARATOR."file.php";`
chelmertz