Downloading from another server with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
databit
Forum Newbie
Posts: 2
Joined: Thu Jan 18, 2007 3:55 pm

Downloading from another server with PHP

Post 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
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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().
databit
Forum Newbie
Posts: 2
Joined: Thu Jan 18, 2007 3:55 pm

Post 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 ;)
farinspace
Forum Newbie
Posts: 6
Joined: Thu Jan 18, 2007 4:00 pm

Post 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 ...
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
Post Reply