Under an Ubuntu machine I wanted to create a script that allows me to send a file via SFTP. For this I use the following code:
$connection = ssh2_connect('XXX', 22);
if (ssh2_auth_password($connection, 'USER', 'PASS')) {
echo "Authentication Successful!\n";
} else {
die('Authentication Failed...');
}
for a first resulting:
Warning: ssh2_auth_password(): Authentication failed for ...
开发者_StackOverflow中文版I thought from reading a lot of tutorials that extending ssh2.so missing. So I installed it and still no results.
Instead of ssh2 based functions I would suggest phpseclib, a pure PHP SFTP implementation.
Using phpseclib you can do:
<?php
include('Net/SFTP.php');
$sftp = new Net_SFTP('www.domain.com');
if (!$sftp->login('username', 'password')) {
exit('Login Failed');
}
echo $sftp->pwd() . "\r\n";
$sftp->put('filename.ext', file_get_contents('localfile.data'););
print_r($sftp->nlist());
?>
You can use the stream mechanism: adress the destination file as
$dest = "ftps://user:pwd@server.com/destinationfile.txt"
and then use the standard php copy
function:
copy($from, $dest)
精彩评论