1. Установите расширение SSH, загрузите расширение соответствующей версии по ссылке расширения и поместите его в PHP/ext/. Затем измените php .ini и выведите phpinfo (), что означает успех
2. Код контроллера (CI для фреймворка)
Код класса SFTP:
connection = ssh2_connect($host,$port); If (! $this - > connection) throw new exception ("$host failed to connect to $port port"); } /** * login *@ param string $login_typelogin type *@ param string $username user name *@ param string $password password *@ param string $pub_key public key *@ param string $pri_key private key * @throws Exception] */ public function login($login_type,$username, $password = null,$pub_key = null,$pri_key = null) { switch ($login_type) { Case 'username': // log in with username and password $login_result = ssh2_auth_password($this->connection, $username, $password); break; Case 'pub_key': // public key and private key login $login_result = ssh2_auth_pubkey_file($this->connection,$username,$pub_key,$pri_key); break; } If (! $login "result) throw new exception (" authentication failed "); $this->sftp = ssh2_sftp($this->connection); If (! $this - > SFTP) throw new exception ("failed to initialize SFTP"); return true; } /** *Upload file *@ param string $local_filelocal file *@ param string $remote_file remote file * @throws Exception */ public function upload_file($local_file, $remote_file) { $is_true = ssh2_scp_send($this->connection, $local_file, $remote_file, 0777); return $is_true; } /** *Download File * @param $local_file * @param $remote_file */ public function down_file ($local_file, $remote_file) { ssh2_scp_recv($this->connection, $remote_file, $local_file); } /** *Determine whether the folder exists *@ param string $dir directory name * @return bool */ public function dir_exits ($dir) { return file_exists("ssh2.sftp://$this->sftp".$dir); } /** *Create directory *@ param string $path example '/ home / username / newdir' *@ param int $auth default permissions of 0777 */ Public function SSH2? SFTP? Mchkdir ($path, $auth = 0777) // use to create a directory loop { $end = ssh2_sftp_mkdir($this->sftp, $path,$auth,true); If ($end! = = true) throw new exception ('folder creation failed '); } /** *Directory rename *@ param string $dir example: '/ home / username / newnamedir' *$dir example: / var / file / image * @return bool */ public function rename ($old_dir,$new_dir) { $is_true = ssh2_sftp_rename($this->sftp,$old_dir,$new_dir); return $is_true; } /** *Delete file *@ param string $dir example: '/ home / username / dirname / filename' *$dir example: / var / file / image/404NotFound.png * @return bool */ public function del_file ($dir) { $is_true = ssh2_sftp_unlink($this->sftp,$dir); return $is_true; } /** *Get files under folder *@ param string $remote_file path example: / var / file / image * @return array */ public function scan_file_system($remote_file) { $sftp = $this->sftp; $dir = "ssh2.sftp://$sftp$remote_file"; $tempArray = array(); $handle = opendir($dir); //List of all files while (false !== ($file = readdir($handle))) { if (substr("$file", 0, 1) != "."){ if(is_dir($file)){ // $tempArray[$file] = $this->scanFilesystem("$dir/$file"); } else { $tempArray[]=$file; } } } closedir($handle); return $tempArray; } }
Ссылка на ссылку: https://www.php.net/manual/zh