NYCPHP Meetup

NYPHP.org

[nycphp-talk] Copying file from HTTP/FTP using copy()

David Krings ramons at gmx.net
Sat Apr 28 07:32:17 EDT 2007


Hi!

Here is what I like to do:
Get a user entry (text string) that is either an HTTP or FTP url to a 
file, for example
http://my.server.gov/download this.zip
or
ftp://username:password@your.server.gov/download that.zip
and then use the copy() function to download it and store it locally on 
my server.

This is the piece of my code that is supposed to do the copying:

if ($nourl == FALSE) {
   // Make local zip file path and name
   $localzipfile = sessiondir.DIRECTORY_SEPARATOR.$sessionid.".zip";
   // urlencode url
   $urlupload = urlencode($urlupload);
   // Copy file
   if(!copy($urlupload, $localzipfile)) $nourl = TRUE;
}
   // Check if error occured
   if($nourl == TRUE) {
   errmessage("OUCH - Downloading the file from ".$urlupload.
              " to temporary storage failed! Try again!");
}

$nourl is an error flag, $localzipfile is the path and name of the local 
file (directory exists, I created it earlier), $urlupload is the 
submission from the user (file on my own web server, if I put the url in 
a browser, the download works), errmessage is a function that displays a 
nice  error message and some buttons to go back.
The PHP manual claims that copy() handles the same wrappers that fopen 
supports. Unfortunately, the response from copy() is either 'worked' or 
'did not work', not much to go by.

The questions of the day:
Why does copy($urlupload, $localzipfile) fail?
Is copy() even the right command to use?

I found a piece of code here
http://sandalian.com/11/php/copy-remote-file-into-local-directory.htm
but I wonder if that works for binary files as well.

As usual, any help is greatly appreciated.

David



More information about the talk mailing list