1. 程式人生 > >linux下用scp命令在兩個服務器之間傳輸文件,利用php_scp函數進行文件傳輸

linux下用scp命令在兩個服務器之間傳輸文件,利用php_scp函數進行文件傳輸

evc 在操作 path send 返回值 遠程 false cal 上傳

在linux下利用scp進行文件傳輸,

從服務器下載文件

scp [email protected]:/path/filename  /path/filename

上傳本地文件到服務器

scp /path/filename [email protected]:/path/filename

從服務器下載整個目錄

scp -r [email protected]:remote_dir/  /path/

上傳目錄到服務器

scp  -r /dir [email protected]:remote_dir

以上操作在執行時都會提示你輸入密碼,輸入密碼後就會成功執行。

但是這些只適合在操作linux服務器時使用,如何在程序中執行呢?

在PHP就用到了php_scp_send和php_scp_revc函數

php_scp_send是向另一個服務器傳輸文件,php_scp_revc則是下載文件。

這兩個函數要結合php_ssh2組件使用。

$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.

linux下用scp命令在兩個服務器之間傳輸文件,利用php_scp函數進行文件傳輸