We help IT Professionals succeed at work.

How can I transfer a file from one site to another using PHP?

davideo7
davideo7 asked
on
How can I transfer a file from one site to another using PHP?  I own both sites, and the files I'd want to transfer would be image files.

Not sure if this would be a problem or not but I'd be transferring the images from a windows server to a linux server.
Comment
Watch Question

BRONZE EXPERT
Commented:
I don't know exactly what you mean with "from one site to another" but perhaps FTP would do what you expect.
http://php.net/manual/en/ref.ftp.php

M@RS
ya ftp would be better solution .
or you can even pull images using
file_get_contents();

Author

Commented:
I should've been more specific.  Basically I'd want a PHP script which can do this.  So users on one of my servers can have their image transferred to the other server with the click of a button.
I think you mean that you have a user running a PHP script on Server A and you want the image transferred from Server A to Server B.  Is that correct?
(If the user is running on Server B then M@RS's suggestion solves the problem.)
No.  Wait.  I think I have it backwards.
Let's start over.

Which computer is the user using?  On which computer is the source document?
Here are some thoughts:

if user is accessing server a to move files from server a to server b:
$remote = ftp_connect('remote.domain.com');
ftl_login($remote,'ftp_user','ftp_pass');
if( ftp_put($remote,'/path/to/remote','/path/to/local') ) {
   echo 'Successfully transferred files';
} else {
   echo 'File transfer failed!';
}
ftp_close($remote);

Open in new window


if user is accessing server b to move files from server a to server b (while it can also be done with ftp, using ftp_get instead of put, this is the way mars suggested):
$remote = "http://www.domain.com/path/to/image.jpg";
$local = "/usr/bin/path/to/image.jpg";
$image = file_get_contents($remote);
file_put_contents($local,$image);

Open in new window


HTH
Commented:
You could use system() to execute a system command and do a secure copy with it. Something like:

system( scp SourceFile user@host:directory/TargetFile );

*** You might need special permissions to allow apache execute scp.