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


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

從服務器下載文件

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

 

上傳本地文件到服務器

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

從服務器下載整個目錄

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

上傳目錄到服務器      

scp  -r /dir username@servername: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.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM