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.
相關文章
相關標籤/搜索