yii2 文件上传扩展
This fork has the aim to implement some missing features such as multiple fields and simplifying installation
You can see the demo on the krajee website
-
修改composer.json文件
"repositories": [ { "url": "https://github.com/c4ys/yii2-attachments.git", "type": "vcs" } ]
和
"c4ys/yii2-attachments": "*"
to the require section of your
composer.json
file. -
添加模块配置
'modules' => [ 'file' => [ 'class' => 'file\FileModule', 'webUrl' => '/upload/', 'tempPath' => '@common/uploads/temp', 'storePath' => '@common/uploads/store', 'rules' => [ 'maxFiles' => 20, 'maxSize' => 1024 * 1024 * 20 // 20 MB ], ], ],
添加命令行配置
'controllerMap' => [ 'file' => [ 'class' => 'yii\console\controllers\MigrateController', 'migrationPath' => '@file/migrations' ], ],
-
应用数据库迁移
yii migrate
-
Attach behavior to your model (be sure that your model has "id" property)
use yii\helpers\ArrayHelper; /** * Declare file fields */ public $my_field_multiple_files; public $my_field_single_file; /** * Adding the file behavior */ public function behaviors() { return ArrayHelper::merge(parent::behaviors(), [ 'fileBehavior' => [ 'class' => \file\behaviors\FileBehavior::className() ] ]); } /** * Add the new fields to the file behavior */ public function rules() { return ArrayHelper::merge(parent::rules(), [ [['my_field_multiple_files', 'my_field_single_file'], 'file'], ]); }
-
Make sure that you have added
'enctype' => 'multipart/form-data'
to the ActiveForm options -
Make sure that you specified
maxFiles
in module rules andmaxFileCount
onAttachmentsInput
to the number that you want