PHP code example of baha2odeh / yii2-easy-fileupload-behavior

1. Go to this page and download the library: Download baha2odeh/yii2-easy-fileupload-behavior library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

baha2odeh / yii2-easy-fileupload-behavior example snippets


    /**
     * {@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 ////


  $form = ActiveForm::begin(); 

php composer.phar