日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

php通过sftp上传文件

發(fā)布時(shí)間:2023/12/31 php 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php通过sftp上传文件 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

使用sftp需要安裝擴(kuò)展!windows & centos7.6下安裝ssh2的php7.3擴(kuò)展(不同對(duì)應(yīng)版本)

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 "用戶名或密碼錯(cuò)誤";}}// 下載文件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);}//創(chuàng)建目錄public function ssh2_sftp_mchkdir($path) //使用創(chuàng)建目錄循環(huán){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'; //遠(yuǎn)程目錄(需要上傳到的目錄)$config = array("host"=>"IP","username"=>"賬號(hào)","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:本地和遠(yuǎn)程路徑一定要準(zhǔn)確!!!

總結(jié)

以上是生活随笔為你收集整理的php通过sftp上传文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。