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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php挂载webdav,PHP上传文件到WebDav

發布時間:2023/12/8 php 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php挂载webdav,PHP上传文件到WebDav 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

添加到驅動類:

use Think\Upload\Driver\Dav\WDClient;

/**

* @describe webdav驅動類

* @author changliang.wu

* @date 2016-10-31

*/

class Dav {

private $config = array(

'host' ? ? => '', //服務器

'port' ? ? => 21, //端口

'username' => '', //用戶名

'password' => '', //密碼

);

private $error = ''; //上傳錯誤信息

private $rootPath; //上傳文件根目錄

private $link; //WD鏈接

/**

* 構造函數,用于設置上傳根路徑

* @param array ?$config WD配置

*/

public function __construct($config){

/* 默認WD配置 */

$this->config = array_merge($this->config, $config);

/* 登錄WD服務器 */

if(!$this->login()){

E($this->error);

}

}

/**

* 登錄WebDav服務器

* @return boolean

*/

private function login() {

if ($this->link) {

return true;

}

$this->link = new WDClient();

$this->link->set_server($this->config['host']);

$this->link->set_port($this->config['port']);

$this->link->set_user($this->config['username']);

$this->link->set_pass($this->config['password']);

$return = $this->link->open();

if (!$return) {

$this->error = '無法連接到WebDav服務器,username:'.$this->config['username'];

return false;

}

return true;

}

/**

* 檢測上傳根目錄

* @param string $rootpath ? 根目錄

* @return boolean true-檢測通過,false-檢測失敗

*/

public function checkRootPath($rootpath){

$this->rootPath = '/'.trim($rootpath, '/').'/';

if(!$this->link->is_dir($this->rootPath)){

$this->error = '上傳根目錄不存在!';

return false;

}

return true;

}

/**

* 檢測上傳目錄

* @param ?string $savepath 上傳目錄

* @return boolean 檢測結果,true-通過,false-失敗

*/

public function checkSavePath($savepath){

//創建目錄

if ($this->mkDir($savepath)) {

return true;

} else {

$this->error = "目錄 {$savepath} 創建失敗!";

return false;

}

}

/**

* 檢測并創建目錄

* @param unknown $savepath

*/

public function mkdir($savepath) {

//檢測目錄是否存在

$dir = $this->rootPath.$savepath;

if($this->link->is_dir($dir)){

return true;

}

//逐級創建目錄

$pathArr = array_filter(explode('/', $savepath));

$dir = ?rtrim($this->rootPath, '/');

foreach ($pathArr as $value) {

$dir .= '/'.$value;

$isDir = $this->link->is_dir($dir);

if (!$isDir) {

$result = $this->link->mkcol($dir);

}

}

return $result;

}

/**

* 保存指定文件

* @param ?array $file 保存的文件信息

* @return boolean 保存狀態,true-成功,false-失敗

*/

public function save($file) {

$filename = $this->rootPath . $file['savepath'] . $file['savename'];

$result = $this->link->put_file($filename, $file['tmp_name']);

//判斷文件是否上傳成功

if (false === strrpos($result, '20')) {

$this->error = '文件上傳保存錯誤!';

return false;

}

return true;

}

/**

* 刪除文件

* @param unknown $path 包含文件的名字

* @return boolean

*/

public function delete($file) {

//判斷文件是否存在

if (!$this->link->is_file($file)) {

$this->error = '要刪除的文件不存在!';

return false;

}

//刪除文件

$result = $this->link->delete($file);

//判斷文件是否刪除失敗

if (false === strrpos(json_encode($result), '20')) {

$this->error = '文件刪除失敗!';

return false;

}

return true;

}

/**

* 獲取最后一次上傳錯誤信息

* @return string 錯誤信息

*/

public function getError(){

return $this->error;

}

}

調用方式:

/** ? ? ?* 上傳文件 ? ? ?*/ ? ? public function upload() { ? ? ? ? if (isset($_FILES['photo'])) { ? ? ? ? ? ? $ftpConfig ? ? = ? ?array( ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'host' ? ? => '172.16.2.108', //服務器 ? ? ? ? ? ? ? ? ? ? ? ? 'port' ? ? => 80, //端口 ? ? ? ? ? ? ? ? ? ? ? ? 'username' => 'winit', //用戶名 ? ? ?? ? ? ? ? ? ? ? ? 'password' => 'winit2015', //密碼? ? ? ? ? ? ? ); ? ? ? ? ? ? $upload = new Upload(array(), 'Dav', $ftpConfig); ? ? ? ? ? ? $upload->maxSize = 3145728; ? ? ? ? ? ? $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); ? ? ? ? ? ? $upload->rootPath = '/uploads/test/'; ? ? ? ? ? ? $info = $upload->upload(); ? ? ? ? ? ? var_dump($info);die; ? ? ? ? } ? ? ? ? $this->display(); ? ? } ? ?? ? ? /** ? ? ?* 測試刪除環境 ? ? ?*/ ? ? public function delete() { ? ? ? ? $filePath = '/uploads/test/2016-10-31/58170e50a600d.gif'; ? ? ? ? $ftpConfig ? ? = ? ?array( ? ? ? ? ? ? 'host' ? ? => '172.16.2.108', //服務器 ? ? ? ? ? ? 'port' ? ? => 80, //端口 ? ? ? ? ? ? 'username' => 'winit', //用戶名 ? ? ? ? ? ? 'password' => 'winit2015', //密碼 ? ? ? ? ); ? ? ? ? $upload = new Upload(array(), 'Dav', $ftpConfig); ? ? ? ? $return = $upload->delete($filePath); ? ? ? ? if ($return) { ? ? ? ? ? ? exit('刪除成功!'); ? ? ? ? } else { ? ? ? ? ? ? exit($upload->getError()); ? ? ? ? } ? ? }

總結

以上是生活随笔為你收集整理的php挂载webdav,PHP上传文件到WebDav的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。