linux下用scp命令在两个服务器之间传输文件,利用php_scp函数进行文件传输

在linux下利用scp进行文件传输,php

从服务器下载文件linux

scp username@servername:/path/filename  /path/filename

 

上传本地文件到服务器服务器

scp /path/filename username@servername:/path/filename

从服务器下载整个目录ssh

scp -r username@servername:remote_dir/  /path/

上传目录到服务器      函数

scp  -r /dir username@servername:remote_dir

以上操做在执行时都会提示你输入密码,输入密码后就会成功执行。spa

可是这些只适合在操做linux服务器时使用,如何在程序中执行呢?code

在PHP就用到了php_scp_send和php_scp_revc函数server

php_scp_send是向另外一个服务器传输文件,php_scp_revc则是下载文件。blog

这两个函数要结合php_ssh2组件使用。rem

$ssh2 = ssh2_connect($ssh_host, $ssh_port);  //先登录SSH并链接,具体参照php_ssh2链接


//$local_file为本地文件, $remote_file为远程文件
//本地传输文件到远程服务器
$stream=ssh2_scp_send($ssh2, $local_file, $remote_file, 0644);
默认权限为0644,返回值为bool值,true或false.

//从远程服务器下载文件
$stream=ssh2_scp_revc($ssh2, $remote_file, $local_file);
//返回值为返回值为bool值,true或false.
相关文章
相关标签/搜索