Page 1 of 1

PHP File copy Server1 ==>Server 2

Posted: Thu Aug 25, 2005 2:21 pm
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

Posted: Thu Aug 25, 2005 2:31 pm
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()

Posted: Thu Aug 25, 2005 2:53 pm
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!

Posted: Thu Aug 25, 2005 2:59 pm
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);

Posted: Thu Aug 25, 2005 3:05 pm
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)?

Posted: Thu Aug 25, 2005 3:10 pm
by feyd
it sounds like you removed the basename() call..

Posted: Thu Aug 25, 2005 3:21 pm
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?

Posted: Thu Aug 25, 2005 3:57 pm
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.

Posted: Thu Aug 25, 2005 4:00 pm
by scts102
ack...chmod then.

Ok, thank you for your time. I am sorry I am so inexperienced at this.

Posted: Thu Aug 25, 2005 4:02 pm
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... ;)

Posted: Thu Aug 25, 2005 7:47 pm
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