七牛云上传文件,php,Demo

namespace app\common\library\storage\engine;

use think\Exception;

use think\Request;

/**

  • 存储引擎抽象类

  • Class server

  • @package app\common\library\storage\drivers

*/

abstract class Server

{

protected $file;

protected $error;

protected $fileName;

protected $fileInfo;

/**

* 构造函数

* Server constructor.

* @throws Exception

*/

protected function __construct()

{

    // 接收上传的文件

    $this->file = Request::instance()->file('iFile');

    if (empty($this->file)) {

        throw new Exception('未找到上传文件的信息');

    }

    // 生成保存文件名

    $this->fileName = $this->buildSaveName();

    // 文件信息

    $this->fileInfo = $this->file->getInfo();

}

/**

* 文件上传

* @return mixed

*/

abstract protected function upload();

/**

* 返回上传后文件路径

* @return mixed

*/

abstract public function getFileName();

/**

* 返回文件信息

* @return mixed

*/

public function getFileInfo()

{

    return $this->fileInfo;

}

/**

* 返回错误信息

* @return mixed

*/

public function getError()

{

    return $this->error;

}

/**

* 生成保存文件名

*/

private function buildSaveName()

{

    // 要上传图片的本地路径

    $realPath = $this->file->getRealPath();

    // 扩展名

    $ext = pathinfo($this->file->getInfo('name'), PATHINFO_EXTENSION);

    // 自动生成文件名

    return date('YmdHis') . substr(md5($realPath), 0, 5)

        . str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT) . '.' . $ext;

}

}

namespace app\common\library\storage\engine;

use Qiniu\Auth;

use Qiniu\Storage\UploadManager;

/**

  • 七牛云存储引擎

  • Class Qiniu

  • @package app\common\library\storage\engine

*/

class Qiniu extends Server

{

private $config;

/**

* 构造方法

* Qiniu constructor.

* @param $config

* @throws \think\Exception

*/

public function __construct($config)

{

    parent::__construct();

    $this->config = $config;

}

/**

* 执行上传

* @return bool|mixed

* @throws \Exception

*/

public function upload()

{

    // 要上传图片的本地路径

    $realPath = $this->file->getRealPath();

    // 构建鉴权对象

    $auth = new Auth($this->config['access_key'], $this->config['secret_key']);

    // 要上传的空间

    $token = $auth->uploadToken($this->config['bucket']);

    // 初始化 UploadManager 对象并进行文件的上传

    $uploadMgr = new UploadManager();

    // 调用 UploadManager 的 putFile 方法进行文件的上传

    list($result, $error) = $uploadMgr->putFile($token, $this->fileName, $realPath);

    if ($error !== null) {

        $this->error = $error->message();

        return false;

    }

    return true;

}

/**

* 返回文件路径

* @return mixed

*/

public function getFileName()

{

    return $this->fileName;

}

}

你可能感兴趣的:(七牛云上传文件,php,Demo)