how to copy a photo from a given URL?

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
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

how to copy a photo from a given URL?

Post by jasongr »

Hello

I am given a URL to a photo on some server
I would like to write a script that copies that photo into some folder in my own server
Can anyone suggests a method to doing that?

regards
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

// assume $url is the remote file url
$fp = fopen('/your/path/'.basename($url),'wb');
if(!$fp)
{
  fwrite($fp, file_get_contents($url));
  fclose($fp);
}
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post by jasongr »

Here is a more detailed explanation

All the URLs in this example are fictious

I am now running in http://1.1.1.1/a.php

this file should copy somehow a photo that resides in a specific folder under the http://1.1.1.1/ URL into another specific folder
in a different server at http://2.2.2.2/
For example, photo http://1.1.1.1/Photos/1.jpg needs to be copied to http://2.2.2.2/OtherPhotos/1.jpg

I thought about having http://1.1.1.1/a.php trigger some php file in http://2.2.2.2/ like http://2.2.2.2/b.php
passing to it the URL of the photo that needs to be copied.
For example: http://2.2.2.2/b.php?url=http://1.1.1.1/Photos/1.jpg

b.php will then copy the photo from http://1.1.1.1/ to the correct folder in http://2.2.2.2/

The problem is that I need to the flow of the code to continue in http://1.1.1.1/a.php after the copying takes place
Is this possible?

regards
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Why not use ftp?
Correct me if I'm wrong, but it sounds like your stealing someone's images. :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the posted code allows for such a thing, as it illustrates..
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Maybe he's building a Google Image Search clone. =P
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post by jasongr »

I found a solution using file_get_contents

I am actually writing this as a fix to my code
I have code that resides on one of my server that does image processing. The code expects the image to be
local on the same machine it is at
However the invoking code may run on some other server, so I need some way to copy an image that the user uploads
from one server to the server on which the image processing code is at

I am not stealing any images

thanks for the help
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

can we mark this solved jasongr?
Post Reply