PHP上传文件到WebDav

来源:互联网 发布:淘宝类目需要品牌授权 编辑:程序博客网 时间:2024/06/05 07:31

添加到驱动类:

<?php

namespace Think\Upload\Driver;
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());
        }
    }



1 0
原创粉丝点击