欢迎访问 生活随笔!

凯发ag旗舰厅登录网址下载

当前位置: 凯发ag旗舰厅登录网址下载 > 编程语言 > php >内容正文

php

php sftp文件上传 文件上传 -凯发ag旗舰厅登录网址下载

发布时间:2023/12/31 php 39 豆豆
凯发ag旗舰厅登录网址下载 收集整理的这篇文章主要介绍了 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
/*** sftp文件上传*//*** sftp 操作類* class sftp*/ class sftp {/*** 連接對象* @var false|resource*/private $connection;/*** 操作對象* @var sftp*/private $sftp;/*** ip* @var mixed*/private $host;/*** 端口* @var mixed*/private $port;/*** 賬號* @var mixed*/private $username;/*** 密碼* @var mixed*/private $password;/*** 初始化sftp連接* sftp constructor.* @param $params 鏈接參數* @throws exception*/public function __construct($params){$this->host = $params['host'];$this->port = $params['port'];$this->username = $params['username'];$this->password = $params['password'];try{$this->connection = ssh2_connect($this->host, $this->port);}catch (exception $e){//throw an exception added by khailthrow new exception("{$this->host} 連接 {$this->port} 埠失敗");}if (!$this->connection) {//the connection errors are recorded in the log file added by khailthrow new exception("{$this->host} 連接 {$this->port} 埠失敗");}$this->login('username');}/*** 登錄* @param string $login_type 登錄類型* @param string $username 用戶名* @param string $password 密碼* @param string $pub_key 公鑰* @param string $pri_key 私鑰* @throws exception]*/public function login($login_type = 'username', $pub_key = null, $pri_key = null){switch ($login_type) {case 'username': //通過用戶名密碼登錄$login_result = ssh2_auth_password($this->connection, $this->username, $this->password);break;case 'pub_key': //公鑰私鑰登錄$login_result = ssh2_auth_pubkey_file($this->connection, $this->username, $pub_key, $pri_key);break;}if (!$login_result) {//add authentication failed message to log added by khailthrow new exception("身份驗證失敗");}$this->sftp = ssh2_sftp($this->connection);if (!$this->sftp) {//'failed to initialize sftp' recorded in the log added by khailthrow new exception("初始化sftp失敗");}return true;}/*** 上傳文件* @param string $local_file 本地文件* @param string $remote_file 遠程文件* @throws exception*/public function upload_file($local_file, $remote_file){$sftp = $this->sftp;$true = copy($local_file, "ssh2.sftp://$sftp/" . $remote_file);return $true;// $is_true = ssh2_scp_send($this->connection, $local_file, $remote_file, 0777);// return $is_true;}/*** 下載文件* @param $local_file 本地文件* @param $remote_file 遠程文件*/public function download_file($local_file, $remote_file){$sftp = $this->sftp;$resource = "ssh2.sftp://$sftp/" . $remote_file;$is_true = copy($resource, $local_file);return $is_true;// $true = ssh2_scp_recv($this->connection, $remote_file, $local_file);// return $true;}/*** 判斷文件夾是否存在* @param string $dir 目錄名稱* @return bool*/public function dir_exits($dir){return file_exists("ssh2.sftp://$this->sftp" . $dir);}/*** 創建目錄* @param string $path 例子 '/home/username/newdir'* @param int $auth 默認 0777的權限*/public function mkdir($path, $auth = 0777) //使用創建目錄循環{$end = ssh2_sftp_mkdir($this->sftp, $path, $auth, true);if ($end !== true) throw new exception('文件夾創建失敗');}/*** 目錄重命名* @param string $dir 例子:'/home/username/newnamedir'* $dir 示例:/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;}/*** 刪除文件* @param string $dir 例子:'/home/username/dirname/filename'* $dir 示例:/var/file/image/404notfound.png* @return bool*/public function del_file($dir){$is_true = ssh2_sftp_unlink($this->sftp, $dir);return $is_true;}/*** 獲取文件夾下的文件* @param string $remote_file 文件路徑 例:/var/file/image* @return array*/public function get_list($remote_file){$sftp = $this->sftp;$dir = "ssh2.sftp://$sftp$remote_file";$temparray = array();$handle = opendir($dir);// 所有的文件列表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;} }

总结

以上是凯发ag旗舰厅登录网址下载为你收集整理的的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图