开发者

Send file by sftp

开发者 https://www.devze.com 2023-02-17 04:57 出处:网络
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:

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消