davidxu / yii2-upload
基于plupload的Yii2上传组件
1.0.11
2023-11-20 18:18 UTC
Requires
- php: >=7.1
- davidxu/yii2-base: ^1.0
- npm-asset/plupload: ~2.3
- yiisoft/yii2: ~2.0.0
Suggests
- npm-asset/qiniu-js: ^3.4.1
- php-ffmpeg/php-ffmpeg: ^1.0
README
这是一个使用plupload的Yii2上传小部件,包括'本地'上传和'七牛云存储'上传
功能
使用此小部件,您可以直接上传文件到本地服务器或远程七牛云存储桶
它还支持通过使用七牛QETag的二次上传功能。
启用分块上传。如果自定义的chunkSize大于系统大小(get_cfg_var('upload_max_filesize')),将使用系统上传的最大文件大小
您目前可以上传文件到本地服务器或七牛Kodo。
有关更多七牛Kodo策略,请参阅七牛网站。
我们使用自定义函数进行视频/图片上传和插入。
如果设置了缓存,您可以通过Yii::$app->cache->get($info['path'])获取上传文件信息,并使用yii\caching\TagDependency::invalidate($cache, $info['path'] . $info['hash'])来使此键无效
安装
通过composer安装此扩展是首选方式。
运行以下命令
php composer.phar require --prefer-dist davidxu/yii2-upload "*"
或者
"davidxu/yii2-upload": "*"
将以下内容添加到您的composer.json文件的要求部分。
使用方法
如果您想将文件信息存储在数据库中(MySQL/MariaDB),请通过以下命令执行迁移文件
yii migrate/up @davidxu/base/migrations
然后只需在代码中简单使用即可
本地上传
在视图中
<?php use davidxu\upload\Upload; use yii\helpers\Url; use davidxu\base\enums\AttachmentTypeEnum; // without ActiveForm echo Upload::widget([ 'model' => $model, 'attribute' => 'image_src', 'name' => 'image_src', // If no model and attribute pointed 'url' => Url::to('@web/upload/local'), 'clientOptions' => [ 'foo' => 'bar', ], 'useFancyUI' => true, // default true 'maxFiles' => 3, 'acceptedFiles' => 'image/*', 'uploadBasePath' => 'uploads/', // for single file, 'existFiles' => [ 'id' => 1, 'name' => 'some_name.jpg', 'path' => 'some_path_for_file', 'poster' => 'some_poster', // for video or audio placeholder 'file_type' => AttachmentTypeEnum::TYPE_IMAGES, 'size' => 1111, ], // for multiple files // 'existFiles' => [ // [ // 'id' => 1, // 'name' => 'some_name.jpg', // 'path' => 'some_path_for_file', // 'poster' => 'some_poster', // 'file_type' => AttachmentTypeEnum::TYPE_IMAGES, // 'size' => 1111, // ], [ // 'id' => 2, // 'name' => 'some_other_name.jpg', // 'path' => 'some_path_for_other_file', // 'poster' => 'some_poster', // for video or audio placeholder // 'file_type' => AttachmentTypeEnum::TYPE_IMAGES, // 'size' => 2222, // ], // ], 'storeInDB' => true, // return file id in DB to image url instead of file url if true, migrate model db first. default false 'metaData' => ['foo' => 'bar',], 'crop' => true, // default false, if true, the 'maxFiles' will be forced to 1 'aspectRatio' => 16 / 9, // default 1 /1 'chunkSize' => 4 * 1024 * 1024, // If `chunkSize` more than system size (`get_cfg_var('upload_max_filesize')`), system upload max filesize will be used 'secondUpload' => true, // if true, `getHashUrl` should be set 'getHashUrl' => Url::to('@web/upload/get-hash'), ]); ?> <?php // with ActiveForm echo $form->field($model, 'image_src') ->widget(Upload::class, [ 'url' => Url::to('@web/upload/local'), 'maxFiles' => 3, 'acceptedFiles' => 'image/*', 'uploadBasePath' => 'uploads/', // for single file, 'existFiles' => [ 'id' => 1, 'name' => 'some_name.jpg', 'path' => 'some_path_for_file', 'poster' => 'some_poster', // for video or audio placeholder 'file_type' => AttachmentTypeEnum::TYPE_IMAGES, 'size' => 1111, ], // for multiple files // 'existFiles' => [ // [ // 'id' => 1, // 'name' => 'some_name.jpg', // 'path' => 'some_path_for_file', // 'poster' => 'some_poster', // 'file_type' => AttachmentTypeEnum::TYPE_IMAGES, // 'size' => 1111, // ], [ // 'id' => 2, // 'name' => 'some_other_name.jpg', // 'path' => 'some_path_for_other_file', // 'poster' => 'some_poster', // for video or audio placeholder // 'file_type' => AttachmentTypeEnum::TYPE_IMAGES, // 'size' => 2222, // ], // ], // .... ]);?>
在上传控制器中
use davidxu\dropzone\actions\LocalAction; use davidxu\dropzone\models\Attachment; use yii\web\Controller; class UploadController extends Controller { public function actions(): array { $actions = parent::actions(); return ArrayHelper::merge([ 'local' => [ 'class' => LocalAction::class, 'url' => Yii::getAlias('@web/uploads'), // default: ''. stored file base url, 'fileDir' => Yii::getAlias('@webroot/uploads'), // default: '@webroot/uploads'. file store in this directory, 'allowAnony' => true, // default false 'attachmentModel' => Attachment::class, ], ], $actions); } }
七牛上传
在视图中
<?php use davidxu\upload\Upload; use yii\helpers\Url; echo Upload::widget([ 'model' => $model, 'attribute' => 'image_src', 'name' => 'image_src', // If no model and attribute pointed 'drive' => UploadTypeEnum::DRIVE_QINIU, // ...... (refer to local config in view) ]); ?> <?php // with ActiveForm echo $form->field($model, 'image_src') ->widget(Upload::class, [ 'drive' => UploadTypeEnum::DRIVE_QINIU, 'qiniuBucket' => Yii::$app->params['qiniu.bucket'], 'qiniuAccessKey' => Yii::$app->params['qiniu.bucket'], 'qiniuSecretKey' => Yii::$app->params['qiniu.bucket'], 'qiniuCallbackUrl' => Yii::$app->params['qiniu.bucket'], // default 'qiniuCallbackBody' here, you can modify them. // 'qiniuCallbackBody' => [ // 'drive' => UploadTypeEnum::DRIVE_QINIU, // 'specific_type' => '$(mimeType)', // 'file_type' => '$(x:file_type)', // 'path' => '$(key)', // 'hash' => '$(etag)', // 'size' => '$(fsize)', // 'name' => '$(fname)', // 'extension' => '$(ext)', // 'member_id' => '$(x:member_id)', // 'width' => '$(imageInfo.width)', // 'height' => '$(imageInfo.height)', // 'duration' => '$(avinfo.format.duration)', // 'store_in_db' => '$(x:store_in_db)', // 'upload_ip' => '$(x:upload_ip)', // ]; // ...... (refer to local config in view) ]);?>
在上传控制器中
use davidxu\dropzone\actions\QiniuAction; use davidxu\dropzone\models\Attachment; use yii\web\Controller; use yii\web\BadRequestHttpException; class UploadController extends Controller { /** * @throws BadRequestHttpException */ public function beforeAction($action): bool { $currentAction = $action->id; $novalidateActions = ['qiniu']; if(in_array($currentAction, $novalidateActions)) { // disable CSRF validation $action->controller->enableCsrfValidation = false; } parent::beforeAction($action); return true; } public function actions(): array { $actions = parent::actions(); return ArrayHelper::merge([ 'qiniu' => [ 'class' => QiniuAction::class, 'url' => Yii::getAlias('@web/uploads'), // default: ''. stored file base url, 'allowAnony' => true, // default false 'attachmentModel' => Attachment::class, ], ], $actions); } }
祝您玩得开心!