运用SSH协定举行FTP传输的协定叫SFTP(平安文件传输)Sftp和Ftp都是文件传输协定。
区分:
sftp是ssh内含的协定(ssh是加密的telnet协定),只需sshd服务器启动了,它就可用,而且sftp平安性较高,它本身不需要ftp服务器启动。 sftp = ssh + ftp(平安文件传输协定)。(引荐进修:PHP编程从入门到通晓)
因为ftp是明文传输的,没有平安性,而sftp基于ssh,传输内容是加密过的,较为平安。现在收集不太平安,之前用telnet的都改用ssh2(SSH1已被破解)。
sftp这个东西和ftp用法一样。然则它的传输文件是经由过程ssl加密了的,纵然被截获了也没法破解。而且sftp比拟ftp功用要多一些,多了一些文件属性的设置
// 注重这里只是为了引见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'])); } // 传输数据 传输层协定,取得数据 true or false public function download($remote, $local,$mode = 'auto'){ return $result = @ftp_get($this->conn, $localpath, $remotepath, $mode); } // 传输数据 传输层协定,上传数据 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'); /*依据上面的三个协定写出基于ssh 的ftp 类 我们晓得举行身份认证的体式格局有两种:公钥;暗码 ; (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){ // 用户认证协定 $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 ; } // 传输数据 传输层协定,取得数据 public function download($remote, $local){ return ssh2_scp_recv($this->conn_, $remote, $local); } //传输数据 传输层协定,写入ftp服务器数据 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);
以上就是php衔接sftp有什么用的细致内容,更多请关注ki4网别的相干文章!