您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
三六零分类信息网 > 阿里分类信息网,免费分类信息发布

阿里云OSS轻量级Driver

2026/5/20 4:39:20发布6次查看
不用加载官方那一大坨sdk了,简单一点
看过网上的一些例子都过于复杂,包含了官方的phpsdk那一大坨东西,理论上我只需要简单的上传文件而已,不需要那么多东西,于是自己写了一下。
以下代码保存到/yourpath/thinkphp/library/think/upload/driver/oss.class.php即可 '', //阿里云access key id
        'access_key' => '', //阿里云access key secret
        'bucket'     => '', //空间名称
        'timeout'    => 90, //超时时间
    );
/**
     * 构造函数,用于设置上传根路径
     * @param array  $config ftp配置
     */
    public function __construct($config) {
        /* 默认ftp配置 */
        $this->config = array_merge($this->config, $config);
    }
/**
     * 检测上传根目录(阿里云上传时支持自动创建目录,直接返回)
     * @param string $rootpath   根目录
     * @return boolean true-检测通过,false-检测失败
     */
    public function checkrootpath($rootpath) {
        /* 设置根目录 */
        $this->rootpath = trim($rootpath, './') . '/';
        return true;
    }
/**
     * 检测上传目录(阿里云上传时支持自动创建目录,直接返回)
     * @param  string $savepath 上传目录
     * @return boolean          检测结果,true-通过,false-失败
     */
    public function checksavepath($savepath) {
        return true;
    }
/**
     * 创建文件夹 (阿里云上传时支持自动创建目录,直接返回)
     * @param  string $savepath 目录名称
     * @return boolean          true-创建成功,false-创建失败
     */
    public function mkdir($savepath) {
        return true;
    }
/**
     * 保存指定文件
     * @param  array   $file    保存的文件信息
     * @param  boolean $replace 同名文件是否覆盖
     * @return boolean          保存状态,true-成功,false-失败
     */
    public function save($file, $replace = true) {
        $_headers = array('content-type: ' . $file['type']);
        $resource = fopen($file['tmp_name'], 'r');
$path = $this->rootpath . $file['savepath'] . $file['savename'];
$uri = /{$this->config['bucket']}{$path};
        $ch  = curl_init('http://' . self::oss_host . $uri);
$date = gmdate('d, d m y h:i:s \g\m\t');
array_push($_headers, date: {$date});
$sign_string = put\n\n . $file['type'] . \n . $date . \n . $uri;
        $sign        = $this->hex_to_base64(hash_hmac('sha1', $sign_string, $this->config['access_key']));
array_push($_headers, 'authorization: oss ' . $this->config['access_id'] . ':' . $sign);
fseek($resource, 0, seek_end);
        $length = ftell($resource);
        fseek($resource, 0);
array_push($_headers, content-length: {$length});
curl_setopt($ch, curlopt_customrequest, put);
        curl_setopt($ch, curlopt_post, 1);
        curl_setopt($ch, curlopt_infile, $resource);
        curl_setopt($ch, curlopt_infilesize, $length);
        curl_setopt($ch, curlopt_httpheader, $_headers);
        curl_setopt($ch, curlopt_timeout, $this->config['timeout']);
        curl_setopt($ch, curlopt_header, 1);
        curl_setopt($ch, curlopt_returntransfer, 1);
        curl_setopt($ch, curlopt_followlocation, 0);
        $response = curl_exec($ch);
        $status   = curl_getinfo($ch, curlinfo_http_code);
        fclose($resource);
        if ($status == 200) {
            return true;
        } else {
            $this->error = $response;
            return false;
        }
    }
/**
     * 获取最后一次上传错误信息
     * @return string 错误信息
     */
    public function geterror() {
        return $this->error;
    }
private function hex_to_base64($str) {
        $result = '';
for ($i = 0; $i             $result .= chr(hexdec(substr($str, $i, 2)));
        }
return base64_encode($result);
    }
}使用方法也比较简单,如果只有一个bucket则可以把配置写入config.php里,内容如下'file_upload_type' => 'oss',
'upload_type_config' => array(
    'access_id' => 'access_id', //阿里云access key id
    'access_key' => 'access_key', //阿里云access key secret
    'bucket' => 'bucket' //阿里云的bucket
),如果你需要将不同的文件存入不同的bucket则需要在上传方法中采用动态配置$config     = array(
    'maxsize'  => 0,
    'exts'     => array('jpg', 'jpeg', 'png'),
    'savename' => array('uniq_photo_name', '__file__'),
    'rootpath' => '/',
);
$oss_config = array(
    'access_id'  => 'access_id', //阿里云access key id
    'access_key' => 'access_key', //阿里云access key secret
    'bucket'     => 'bucket'
);
$upload     = new \think\upload($config, 'oss', $oss_config);
ad:真正免费,域名+虚机+企业邮箱=0元
阿里分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product