Page 1 of 1

Downloading from another server with PHP

Posted: Thu Jan 18, 2007 4:04 pm
by databit
What would be the code for downloading from a link to the server then displaying the new link.
Example
I am browsing the internets and I find http://www.yoursite.com/thisisawesomebu ... ternet.jpg
Normally if I wanted to post that picture to my site I would either have to link directly to their image and steal their bandwidth (very much frowned upon) or right click save as and then upload it to my ftp. Then post it. All which is a bit of a hassle.

I want to be able to go to a webform and paste in the url to the picture. Click submit. The php downloads the pic to the server and tells me the new address is http://www.mysite.com/thisisawesomebuta ... ternet.jpg.

I'm guessing this is pretty basic but I'm not sure where to start looking

Posted: Thu Jan 18, 2007 4:11 pm
by aaronhall
You can use copy() to move the file from the remote site onto your own. The image would have to be in a public directory, and you would have to check for filename collisions with file_exists().

Posted: Thu Jan 18, 2007 4:18 pm
by databit
Ah perfect. Thanks!

Another question that will probably answer itself. When you typed copy() and file_exists() does it automatically link to the php.net page for it.
You don't actually have to answer this because when I view this post I will know because I know I didn't include hrefs ;)

Posted: Thu Jan 18, 2007 4:19 pm
by farinspace
you can do the following (assuming you dont have access to the secondary server, just your own):

Code: Select all

$contents= file_get_contents("http://forums.devnetwork.net/templates/subSilver/images/logo_phpBB.gif");
file_put_contents("logo_phpBB.gif",$contents);
the above is just a basic test, you would obviously have to make sure file url wrapper is enabled and do some further testing to ensure all is stable, you would have to do some image renaming when putting ...

Posted: Thu Jan 18, 2007 4:27 pm
by aaronhall
databit wrote:Ah perfect. Thanks!

Another question that will probably answer itself. When you typed copy() and file_exists() does it automatically link to the php.net page for it.
You don't actually have to answer this because when I view this post I will know because I know I didn't include hrefs ;)
It's actually a cool greasemonkey script that was written for this very purpose. You'll need to install the greasemonkey extension on firefox if you don't already have it. All the links (including one to the script itself) are listed here.