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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jfinal save 超过9个字段以上
- 下一篇: 基恩士编程c语言,基恩士xg7000系列