hossein142001 / yii2-flysystem-wrapper
yii2 flysystem wrapper
dev-master
2022-11-13 07:28 UTC
Requires
This package is not auto-updated.
Last update: 2024-09-30 13:00:08 UTC
README
yii2 flysystem wrapper. Flysystem 是一种文件系统抽象,允许您轻松地将本地文件系统与远程文件系统进行交换。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
php composer.phar require --prefer-dist hossein142001/yii2-flysystem-wrapper "*"
或
"hossein142001/yii2-flysystem-wrapper": "*"
将以下内容添加到您的 composer.json 文件的 require 部分中。
然后升级迁移
php yii migrate/up --migrationPath=vendor/hossein142001/yii2-flysystem-wrapper/migrations
用法/功能
扩展安装完成后,只需在代码中通过以下方式使用它
将 "fs" 添加到组件中
'fs' => [ 'class' => 'Integral\Flysystem\Adapter\PDOAdapter', // or other adapters 'tableName' => 'file_storage' ],
上传示例代码
<?php $model = new MyModel(); if (Yii::$app->request->isPost) { $model->files = UploadedFile::getInstancesByName('files'); if ($model->validate()) { $data = [ 'path' => '@common/files', 'context' => '025', 'version' => '1', 'metadata' => ['meta' => 1, 'meta2' => 2, 'meta3' => 3], ]; return FlysystemWrapper::upload($model->files, $data); } return $model; } ?>
注意:在模型文件中规则 "maxFiles" 是必需的
<?php [['files'], 'file', 'skipOnEmpty' => false, 'maxFiles' => 10, 'extensions' => 'txt, jpg'] ?>
通过哈希键获取文件
<?php $hashKey = 'XXX'; return FlysystemWrapper::getByHash($hashKey); ?>
通过哈希键读取文件
<?php $hashKey = 'XXX'; return FlysystemWrapper::readByHash($hashKey); ?>
通过哈希键删除文件
<?php $hashKey = 'XXX'; return FlysystemWrapper::deleteByHash($hashKey); ?>
注意:删除方法是逻辑上的
通过元数据或文件模型特殊属性搜索文件(多个)
<?php $params = ['meta1' => 1, 'version' => 2]; return FlysystemWrapper::searchByParams($params); ?>