NYCPHP Meetup

NYPHP.org

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

Jon Baer jonbaer at jonbaer.com
Sat Apr 28 08:55:54 EDT 2007


You would probably be better off using the libcurl commands ...

http://us.php.net/curl

As getinfo is one of the better options to get stats on the transfer  
in question ...

http://us.php.net/manual/en/function.curl-getinfo.php

- Jon

On Apr 28, 2007, at 7:32 AM, David Krings wrote:

> 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
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php




More information about the talk mailing list