通过 SFTP 将文件从本地 Linux 服务器传输到远程 Linux 服务器


Transferring a file from local Linux server to remote Linux server over SFTP

我可以在同一台计算机上使用下面的代码将文件从一个文件夹传输到另一个文件夹,该代码适用于文件。

$file = 'pdf.pdf';
$source_path = 'source/';
$target_path = 'target/';
if (! copy($source_path . $file, $target_path . $file))
{
    exit('Failed to copy ' . $source_path . $file);
}
echo $source_path . $file . ' file copied to ' . $target_path . $file;

我想做什么来将相同的文件传输到使用 SFTP-SSH 的远程 linux 服务器,那么我该怎么做?如果有人问,我找到了很多例子来理解cURL((,ftp_get((,stream_copy_to_stream((,ssh2_sftp((,scp((的逻辑,但由于以前从未做过,因此未能完成。

注意:我下面的设置适用于FileZilla。

target host: https://192.168.32.1/test_folder/
target host: https://mysite.site.com/test_folder/
protocol: SFTP-SSH
port: 2281
username: myusername
password: mypassword

谢谢

我自己从未尝试过,但phpseclib似乎很有前途:http://phpseclib.sourceforge.net/sftp/examples.html#put