php連接sftp的作用以及實例代碼
sftp 協(xié)議
使用SSH協(xié)議進(jìn)行FTP傳輸?shù)膮f(xié)議叫SFTP(安全文件傳輸)Sftp和Ftp都是文件傳輸協(xié)議。
區(qū)別:
sftp是ssh內(nèi)含的協(xié)議(ssh是加密的telnet協(xié)議),只要sshd服務(wù)器啟動了,它就可用,而且sftp安全性較高,它本身不需要ftp服務(wù)器啟動。 sftp = ssh + ftp(安全文件傳輸協(xié)議)。
由于ftp是明文傳輸?shù)?,沒有安全性,而sftp基于ssh,傳輸內(nèi)容是加密過的,較為安全。目前網(wǎng)絡(luò)不太安全,以前用telnet的都改用ssh2(SSH1已被破解)。
sftp這個工具和ftp用法一樣。但是它的傳輸文件是通過ssl加密了的,即使被截獲了也無法破解。而且sftp相比ftp功能要多一些,多了一些文件屬性的設(shè)置
// 注意這里只是為了介紹ftp ,并沒有做驗證 ;
class ftp{
// 初始配置為NULL
private $config =NULL ;
// 連接為NULL
private $conn = NULL;
public function init($config){
$this->config = $config;
}
// ftp 連接
public function connect(){
return $this->conn = ftp_connect($this->config['host'],$this->config['port']));
}
// 傳輸數(shù)據(jù) 傳輸層協(xié)議,獲得數(shù)據(jù) true or false
public function download($remote, $local,$mode = 'auto'){
return $result = @ftp_get($this->conn, $localpath, $remotepath, $mode);
}
// 傳輸數(shù)據(jù) 傳輸層協(xié)議,上傳數(shù)據(jù) true or false
public function upload($remote, $local,$mode = 'auto'){
return $result = @ftp_put($this->conn, $localpath, $remotepath, $mode);
}
// 刪除文件
public function remove($remote){
return $result = @ftp_delete($this->conn_id, $file);
}
}
// 使用
$config = array(
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',
'port' => 21
) ;
$ftp = new Ftp();
$ftp->connect($config);
$ftp->upload('ftp_err.log','ftp_upload.log');
$ftp->download('ftp_upload.log','ftp_download.log');
/*根據(jù)上面的三個協(xié)議寫出基于ssh 的ftp 類
我們知道進(jìn)行身份認(rèn)證的方式有兩種:公鑰;密碼 ;
(1) 使用密碼登陸
(2) 免密碼登陸也就是使用公鑰登陸
*/
class sftp{
// 初始配置為NULL
private $config =NULL ;
// 連接為NULL
private $conn = NULL;
// 是否使用秘鑰登陸
private $use_pubkey_file= false;
// 初始化
public function init($config){
$this->config = $config ;
}
// 連接ssh ,連接有兩種方式(1) 使用密碼
// (2) 使用秘鑰
public function connect(){
$methods['hostkey'] = $use_pubkey_file ? 'ssh-rsa' : [] ;
$con = ssh2_connect($this->config['host'], $this->config['port'], $methods);
//(1) 使用秘鑰的時候
if($use_pubkey_file){
// 用戶認(rèn)證協(xié)議
$rc = ssh2_auth_pubkey_file(
$conn,
$this->config['user'],
$this->config['pubkey_file'],
$this->config['privkey_file'],
$this->config['passphrase'])
);
//(2) 使用登陸用戶名字和登陸密碼
}else{
$rc = ssh2_auth_password( $conn, $this->conf_['user'],$this->conf_['passwd']);
}
return $rc ;
}
// 傳輸數(shù)據(jù) 傳輸層協(xié)議,獲得數(shù)據(jù)
public function download($remote, $local){
return ssh2_scp_recv($this->conn_, $remote, $local);
}
//傳輸數(shù)據(jù) 傳輸層協(xié)議,寫入ftp服務(wù)器數(shù)據(jù)
public function upload($remote, $local,$file_mode=0664){
return ssh2_scp_send($this->conn_, $local, $remote, $file_mode);
}
// 刪除文件
public function remove($remote){
$sftp = ssh2_sftp($this->conn_);
$rc = false;
if (is_dir("ssh2.sftp://{$sftp}/{$remote}")) {
$rc = false ;
// ssh 刪除文件夾
$rc = ssh2_sftp_rmdir($sftp, $remote);
} else {
// 刪除文件
$rc = ssh2_sftp_unlink($sftp, $remote);
}
return $rc;
}
}
$config = [
"host" => "192.168.1.1 ", // ftp地址
"user" => "***",
"port" => "22",
"pubkey_path" => "/root/.ssh/id_rsa.pub", // 公鑰的存儲地址
"privkey_path" => "/root/.ssh/id_rsa", // 私鑰的存儲地址
];
$handle = new SftpAccess();
$handle->init($config);
$rc = $handle->connect();
$handle->getData(remote, $local);
以上就是本次介紹的全部知識點內(nèi)容,感謝大家的學(xué)習(xí)和對腳本之家的支持。
相關(guān)文章
php 記錄進(jìn)行累加并顯示總時長為秒的結(jié)果
用php如何將這些記錄進(jìn)行累加,最后顯示為一個總時長為秒鐘的結(jié)果2011-11-11
支持?jǐn)?shù)組的ADDSLASHES的php函數(shù)
支持?jǐn)?shù)組的ADDSLASHES2010-02-02
PHP基于Redis消息隊列實現(xiàn)發(fā)布微博的方法
這篇文章主要介紹了PHP基于Redis消息隊列實現(xiàn)發(fā)布微博的方法,結(jié)合實例形式分析了php+redis數(shù)據(jù)庫的安裝、連接、讀取、插入等相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
ThinkPHP中公共函數(shù)路徑和配置項路徑的映射分析
這篇文章主要介紹了ThinkPHP中公共函數(shù)路徑和配置項路徑的映射,較為通俗的分析了ThinkPHP中公共函數(shù)路徑和配置項路徑的映射關(guān)系與對應(yīng)修改位置,有助于更好的理解ThinkPHP底層代碼原理,需要的朋友可以參考下2014-11-11

