baha2odeh/yii2-easy-fileupload-behavior 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

baha2odeh/yii2-easy-fileupload-behavior

最新稳定版本:v1.0

Composer 安装命令:

composer require baha2odeh/yii2-easy-fileupload-behavior

包简介

Yii2 Easy File Upload Behavior

README 文档

README

Yii2 Easy File Upload Behavior

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist baha2odeh/yii2-easy-fileupload-behavior "*"

or add

"baha2odeh/yii2-easy-fileupload-behavior": "*"

to the require section of your composer.json file.

you have to not use UploadedFile::getInstance anymore this can be done by this behavior

Usage

Once the extension is installed, simply use it in your code by : add this on your php model

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['images_files','image_file'],'file'], //@TODO add your rules here
        ];
    }

    public function behaviors()
    {
       return [
           [
               'class' => EasyFileUploadBehavior::className(),
               'relations' => [
                   'images_files' => 'images', // images means your HasMany Relation Name
                   'image_file' => 'image', // image means your HasOne Relation Name
               ],
               'uploadCallBack' => function($relationName,UploadedFile $file){
               	   // do your magic here and return one model that you save the image on it
               	   // if return is null file will skipped
               	   $file->saveAs('upload-path/'.$file->name);
                   $image = new Image();
                   $image->filename = $file->name;
                   $image->save(false);
                   return $image;
               }
           ]
       ];
    }


    //// demo only ////

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getImage()
    {
        return $this->hasOne(Image::className(), ['image_id' => 'image_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getArticleImages()
    {
        return $this->hasMany(ArticleImage::className(), ['article_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getImages()
    {
        return $this->hasMany(Image::className(), ['image_id' => 'image_id'])->via('articleImages');
    }
    //// demo only ////

then use it on your form like this

 <?php $form = ActiveForm::begin(); ?>
    <div class="box-body table-responsive">

        <?= $form->field($model, 'image_file')->fileInput() ?>

        <?= $form->field($model, 'images_files[]')->fileInput(['multiple'=>true]) ?>



    </div>
    <div class="box-footer">
        <?= Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success btn-flat']) ?>
    </div>
    <?php ActiveForm::end(); ?>

统计信息

  • 总下载量: 1.8k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2019-04-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固