PHP File copy Server1 ==>Server 2

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
scts102
Forum Newbie
Posts: 23
Joined: Thu Aug 25, 2005 2:15 pm

PHP File copy Server1 ==>Server 2

Post by scts102 »

Hey. First off, I am new to this community, and I hope that I can become an active member.

Second, I have an issue. I need to know how to take an image from one site (For testing purposes, lets say http://www.google.com/intl/en/images/logo.gif ) and copy it to another server (note that the images will most likely be in PNG format, if that makes a difference).

Now, the server that I get the file from, I have no write access to (or ftp). It would be like viewing a standard webpage. The second server, however, I have full access to. The php script runs on the second server.

Is this possible?

I hope I was clear in my explanation.

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

Post by feyd »

it's quite easy :)

PHP 5:

Code: Select all

file_put_contents(basename($url),file_get_contents($url));
PHP 4:

Code: Select all

$fp = fopen(basename($url),'wb');
if($fp) fwrite($fp,file_get_contents($url));
fclose($fp);
this assumes your server has url wrappers turned on. It's not a whole lot harder to do if they aren't.. just a bit more involved ;)


[edit] oops messed an argument to fopen()
scts102
Forum Newbie
Posts: 23
Joined: Thu Aug 25, 2005 2:15 pm

Post by scts102 »

Thank you for the prompt reply.

Sorry to burden you, but I am not very familiar with php....I know echo(), how to use variables, create forms, and access MYSQL, but other than that, I am lost.

Now, I am running php4, so I am using the latter code:
$fp = fopen(basename($url),'wb');
if($fp) fwrite($fp,file_get_contents($url));
fclose($fp);

So, how do I work this code?
Lets say that I have the image link on server 1 stored in $pbb, and I want to copy it to server 2 (where the script is being run) to http://www.example.com/filename.png?

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

Post by feyd »

it goes like this:

Code: Select all

$url = 'http://www.google.com/intl/en/images/logo.gif';

$fp = fopen(basename($url),'wb');
if($fp) fwrite($fp,file_get_contents($url));
fclose($fp);
that should write 'logo.gif' (sans quotes) to the script's location on your server.

basename() pulls the filename out of the URL given to it, in this case: logo.gif
fopen() opens a file stream to a specified file.
file_get_contents() is used to acquire the contents of a file into a single string.
fwrite() writes to a stream, in this case $fp;
fclose() closes a stream ($fp);
scts102
Forum Newbie
Posts: 23
Joined: Thu Aug 25, 2005 2:15 pm

Post by scts102 »

Thanks for the explanation, that helped me understand it :) You have good patience

I updated the script, and got the following error:

Warning: fopen(http://69.25.18.226/69.25.18.226/pb000001.png): failed to open stream: HTTP wrapper does not support writeable connections. in /home/kaotclan/public_html/pbss/finish.php on line 5

Warning: fclose(): supplied argument is not a valid stream resource in /home/kaotclan/public_html/pbss/finish.php on line 7

The ip you see above is the google example that I provided. It is a correct, working link. Now I assume that the fclose() error is tied to the fopen() error (based on experience in other languages)?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it sounds like you removed the basename() call..
scts102
Forum Newbie
Posts: 23
Joined: Thu Aug 25, 2005 2:15 pm

Post by scts102 »

Yea, I did....dont remember doing that. My mind is going :(

Anyway, I get a new error:
Warning: fopen(pb000001.png): failed to open stream: Permission denied in /home/kaotclan/public_html/pbss/finish.php on line 6

Warning: fclose(): supplied argument is not a valid stream resource in /home/kaotclan/public_html/pbss/finish.php on line 8

I take it that Server 1 is not allowing this?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

nope, it's your server. The permissions of the current folder are such that the script is not allowed to write to it.
scts102
Forum Newbie
Posts: 23
Joined: Thu Aug 25, 2005 2:15 pm

Post by scts102 »

ack...chmod then.

Ok, thank you for your time. I am sorry I am so inexperienced at this.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

scts102 wrote:I am sorry I am so inexperienced at this.
Don't worry about it, we're quite used to newb invasions :)

we were all newbs at one point or another... ;)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

haha, the first time i read his post I missed the part about the script running on server 2 and was thinking the script was on server one, I was reading feyd's post going "no, no its all wrong".. heh
Post Reply