I posted a question a little while ago but I asked about FTP, which is the wrong question (doh!). I need to automatically transfer images (e.g. test.jpg) from one server to another using SFTP/SSH.
Could someone please explain how I would do this? I am completely new to this kind of thing so as much information as possible would be really appreciated.
Thanks for any h开发者_StackOverflow中文版elp
<?php
$local_file = "http://localhost/ftptest/logo.png";
$remote_file = "/logo.png";
exec("scp $local_file username:pass@***.**.238.87:$remote_file");
?>
edit:
in the end I found that this works:
<?php
include('Net/SFTP.php');
$image = 'logo.jpg'; //image to be uploaded - needs to be in the same directory as this script e.g. just logo.jpg
$image_contents = file_get_contents($image); // location of image to be uploaded
$sftp = new Net_SFTP('***.**.**.**'); // server address to connect to
if (!$sftp->login('***', '***')) { // server login details
exit('Login Failed');
}
echo $sftp->pwd();
$sftp->put($image, $image_contents); // upload a file $image with the image contents.
?>
Couldn't get the SSH working but hopefully this will help someone in the future :)
As is too often the case here, you've not provided much information about what you are trying to achieve nor the constraints.
Where does the image originate from? How quickly does it need to be replicated? Are the servers equivalent nodes in a cluster?
Since both servers are already talking HTTP, why use a different protocol for transferring content?
As Antonio suggests you could simply exec scp - but this is only going to work if you've got key pairs set up.
A more flexible solution (assuming ssh is a requirement) would be to use the ssh bindings in PHP
A simple way to handle this is to call PHP exec and execute a unix scp call.
exec("scp $local_file user1@somehost:$remote_file");
You can try ssh2_scp_send:
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);
精彩评论