kl83/yii2-file-storage

Yii2 文件存储

v1.2.3 2018-11-24 00:36 UTC

This package is auto-updated.

Last update: 2024-09-07 04:38:41 UTC


README

Latest Stable Version Total Downloads License

模块和小部件用于上传文件。该模块将文件上传到随机目录,保存其名称。文件的 URL、所有者、上传日期和唯一 ID 存储在数据库中。此外,多个文件可以组合成具有自己 ID、所有者和创建日期的文件集。认证用户上传的文件存储在单独的目录中。模块具有管理文件的权限检查。

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一:

php composer.phar require kl83/yii2-file-storage ~1.2.0

或添加以下内容到您的 composer.json 文件的要求部分。

"kl83/yii2-file-storage": "~1.2.0"

然后应用迁移。

模块配置

./yii migrate --migrationPath=@vendor/kl83/yii2-file-storage/migrations

config/web.php

模块动作

...
"modules" => [
    ...
    "filestorage" => "kl83\filestorage\Module",
    ...
]
...

删除文件。

defaut/delete (int $id) : null
$id - id of file to delete

将文件移动到文件集中的某个位置。

defaut/move (int $id, int $afterId) : null
$id - id of file to move
$afterId - set file position to be after that file, if value is empty, then file
    will be the first

上传文件。

defaut/upload (string|array $attributes = null, int $filesetId = null) : json
$attributes - key's of $_FILES array to be save, if not set, then all files to
    be saved
$filesetId - a fileset to which files will be attached,
    if it's -1 then new fileset will be created
return [
    'files' => [
        id and url of file,
    ],
    'fileset' => fileset id,
]

使用 PicWidget

选择并上传一张图片的小部件。

picId 必须是整数或 null

$form->field($model, 'picId')->widget('kl83\filestorage\widgets\PicWidget');

示例模型方法以获取上传的文件

使用 PicSetWidget

public function getPic()
{
    return $this->hasOne('kl83\filestorage\models\File', [ 'id' => 'picId' ]);
}
public function getPicUrl()
{
    return $this->getPic()->url;
}

选择并上传一些图片的小部件。

picSetId 必须是整数属性

$form->field($model, 'picSetId')->widget('kl83\filestorage\widgets\PicSetWidget', [
    'maxImages' => 3, // default is unlimited
]);

示例模型方法以获取上传的文件

许可协议

public function getPicSet()
{
    return $this->hasOne('kl83\filestorage\models\FileSet', [ 'id' => 'picSetId' ]);
}
public function getPics()
{
    // returns array of kl83\filestorage\models\File
    return $this->getPicSet()->getFiles();
}
public function getCover()
{
    $pics = $this->getPics();
    if ( is_array($pics) ) {
        return current($pics);
    }
}
public fucntion getCoverUrl()
{
    return $this->getCover()->url;
}

MIT 许可协议

MIT 许可协议