myzero1 / yii2-upload
为 yii2 应用提供上传功能
1.1.1
2023-08-03 07:41 UTC
Requires
- yii2-starter-kit/yii2-file-kit: @stable
Requires (Dev)
- creocoder/yii2-flysystem: ~0.8
- phpunit/phpunit: ~4.5.0
This package is not auto-updated.
Last update: 2024-09-12 12:26:20 UTC
README
你可以上传文件,只需添加一个小部件来查看。
安装
安装此模块的首选方式是通过 composer。
运行以下命令之一
php composer.phar require myzero1/yii2-upload:1.*
或
"myzero1/yii2-upload": "~1"
将以下内容添加到你的 composer.json
文件的 require 部分中。
设置
模块安装后,只需修改你的应用程序配置如下
return [ 'modules' => [ 'upload' => [ 'class' => 'myzero1\yii2upload\Tools', 'upload' => [ 'basePath' => '@webroot/upload', 'baseUrl' => '@web/upload', ], ], // ... ], // ... ];
使用方法
添加上传小部件,例如以下所示
echo \myzero1\yii2upload\widget\upload\Upload::widget([
'model' => $model,
'attribute' => 'logo',
// 'url' => ['/tools/upload/upload'], // default ['/tools/upload/upload'],
// 'sortable' => true,
// 'maxFileSize' => 200 * 1024, // 200k
// 'minFileSize' => 1 * 1024, // 1k
// 'maxNumberOfFiles' => 1, // default 1,
// 'acceptFileTypesNew' => [], // default ['gif','jpeg','jpg','png'],
// 'acceptFileTypes' => new \yii\web\JsExpression('/(\.|\/)(gif|jpe?g|png)$/i'),// if it is null,the acceptFileTypesNew will working.
// 'showPreviewFilename' => false,
// 'clientOptions' => []
]);
与 ActiveForm 一起使用
echo $form->field($model, 'logo')->widget(
'\myzero1\yii2upload\widget\upload\Upload',
[
// 'url' => ['/tools/upload/upload'], // default ['/tools/upload/upload'],
// 'sortable' => true,
// 'maxFileSize' => 200 * 1024, // 200k
// 'minFileSize' => 1 * 1024, // 1k
// 'maxNumberOfFiles' => 1, // default 1,
// 'acceptFileTypesNew' => [], // default ['gif','jpeg','jpg','png'],
// 'acceptFileTypes' => new \yii\web\JsExpression('/(\.|\/)(gif|jpe?g|png)$/i'),// if it is null,the acceptFileTypesNew will working.
// 'showPreviewFilename' => false,
// 'clientOptions' => []
]
);
然后,您可以通过以下 URL 访问 Upload 测试
http://localhost/path/to/index.php?r=upload/upload/test
或者如果您已启用美观的 URL,您也可以使用以下 URL
http://localhost/path/to/index.php/upload/upload/test