qsnh/think-upload

该包已被废弃,不再维护。未建议替代包。

Thinkphp5 文件上传库。

0.2.5 2016-12-10 14:27 UTC

This package is auto-updated.

Last update: 2018-10-18 11:52:56 UTC


README

Thinkphp5 仅支持本地文件上传功能,但许多项目需要使用七牛或阿里云云存储。因此,我编写了这个库。

首先

application/extra目录下创建upload.php文件,并复制以下代码

return [
    'driver' => 'aliyun',
    /** limit file size,unit:byte,if the value equal 0 the file size will not restricted.  */
    'size' => 1024 * 1024,
    /** allow file extension.if is empty the file extension has no limit. */
    'ext'  => [],
    /** allow file type by file mime.if is empty the file type has no limit. */
    'type' => [],
    'path' => './images/',
    'default' => [
        /** access url */
        'remote_url' => 'http://xx.com/public/',
    ],
    'qiniu' => [
        'access_key' => '',
        'secret_key' => '',
        'bucket' => '',
        /** access url */
        'remote_url' => '',
    ],
    'upyun' => [
        'username' => '',
        'password' => '',
        'bucket' => '',
        /** access url */
        'remote_url' => '',
    ],
    'aliyun' => [
        /** Internal net url or External net url */
        'oss_server' => '',
        'access_key_id' => '',
        'access_key_secret' => '',
        'bucket' => '',
        /** access url */
        'remote_url' => '',
    ],
];

如果使用阿里云驱动,请打开存储桶的读权限。因为存储桶的读权限被关闭,服务器将返回访问被拒绝。

用法

<?php
use Qsnh\think\Upload\Upload;

$upload = new Upload(config('upload'));

// $result = $upload->upload('avatar', '123.jpg');
$result = $upload->upload('avatar'); // first parameter is folder path; second parameter is custom filename(default: null) 

if (!$result) {
    $this->error($upload->getError());
}

halt($result);

upload()方法有一个可选参数。它的默认值是file,它等于文件组件的名称。