当前位置:
凯发ag旗舰厅登录网址下载 >
编程语言
> php
>内容正文
php
php通过sftp上传文件 -凯发ag旗舰厅登录网址下载
凯发ag旗舰厅登录网址下载
收集整理的这篇文章主要介绍了
php通过sftp上传文件
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
使用sftp需要安装扩展!windows & centos7.6下安装ssh2的php7.3扩展(不同对应版本)
sftp类
class sftp {// 初始配置为nullprivate $config = null;// 连接为nullprivate $conn = null;//sftp resourceprivate $ressftp = null;// 初始化public function __construct($config){$this->config = $config;$this->connect();}public function connect(){$this->conn = ssh2_connect($this->config['host'], $this->config['port']);if( ssh2_auth_password($this->conn, $this->config['username'], $this->config['password'])){$this->ressftp = ssh2_sftp($this->conn);}else{return "用户名或密码错误";}}// 下载文件public function downftp($remote, $local){return copy("ssh2.sftp://{$this->ressftp}".$remote, $local);}// 文件上传public function upftp( $local,$remote, $file_mode = 0777){return copy($local,"ssh2.sftp://{$this->ressftp}".$remote);}//创建目录public function ssh2_sftp_mchkdir($path) //使用创建目录循环{ssh2_sftp_mkdir($this->ressftp, $path,0777,true);}//判段目录是否存在public function ssh2_dir_exits($dir){return file_exists("ssh2.sftp://{$this->ressftp}".$dir);} }上传
public function sftpupdate(){//本地文件目录$localpath = "/test.txt"; //本地文件路径$serverpath='/data/test'; //远程目录(需要上传到的目录)$config = array("host"=>"ip","username"=>"账号","port"=>"22","password"=>"密码");try {$sftp = new \app\model\sftp($config);$res = $sftp->ssh2_dir_exits("$serverpath");//如果目录存在直接上传if($res){$sftp->upftp($localpath,$serverpath);}else{$sftp->ssh2_sftp_mchkdir($serverpath);$sftp->upftp($localpath,$serverpath);}return 'ok';} catch (\exception $e) {return $e->getmessage();} }ps:本地和远程路径一定要准确!!!
总结
以上是凯发ag旗舰厅登录网址下载为你收集整理的php通过sftp上传文件的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。
- 上一篇:
- 下一篇: