php sftp文件上传 文件上传
生活随笔
收集整理的這篇文章主要介紹了
php sftp文件上传 文件上传
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
<?php
/*** 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 鏈接參數(shù)* @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("身份驗(yàn)證失敗");}$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 遠(yuǎn)程文件* @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 遠(yuǎn)程文件*/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);}/*** 創(chuàng)建目錄* @param string $path 例子 '/home/username/newdir'* @param int $auth 默認(rèn) 0777的權(quán)限*/public function mkdir($path, $auth = 0777) //使用創(chuàng)建目錄循環(huán){$end = ssh2_sftp_mkdir($this->sftp, $path, $auth, true);if ($end !== true) throw new Exception('文件夾創(chuàng)建失敗');}/*** 目錄重命名* @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;}
}
總結(jié)
以上是生活随笔為你收集整理的php sftp文件上传 文件上传的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cad展点kszd小程序_CAD展点程序
- 下一篇: 动态规划算法php,php算法学习之动态