PHP code example of fabriziocaldarelli / yii2-file-upload

1. Go to this page and download the library: Download fabriziocaldarelli/yii2-file-upload 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/ */

    

fabriziocaldarelli / yii2-file-upload example snippets


'modules' => [
    'fileUploader' => [
        'class' => 'sfmobile\fileUpload\Module',

        // Database table name to save files metadata
        'dbTableName' => 'tbl_file_upload',

        'defaultStorage' => 's3prod',

        'storages' => [
            's3prod' => [
                'class' => 'sfmobile\fileUpload\storages\S3Storage',

                's3Key' => 'PROD',
                's3Secret' => '1234567',
                's3Endpoint' => 'https://mys3',
                's3Bucket' => 'mybucket',
            ],                
            'local' => [
                'class' => 'sfmobile\fileUpload\storages\FileSystemStorage',

                'basePath' =>  '/var/www/vhosts/your_hosting/public_files',
                'baseUrl' =>  '/public_files',
            ],
        ]        
    ],
],

'bootstrap' => ['log', 'fileUploader'],


namespace backend\models;

class Articles extends \yii\db\ActiveRecord
{
    public $formTabularIndex = null; // Needed for file upload with tabular form
    public $photo;  // attribute to store uploaded file

    // Load: initialize the files attached to model
    public function load($data, $formName = null)
    {
        $retLoad = parent::load($data, $formName);

        // Files metadata will be stored with refer_table=articles, section=articles, category=photo and refer_id=model.id
        \sfmobile\fileUpload\FileUploadCore::load($this->formTabularIndex, $this, 'photo', $this->id, 'articles', 'articles', 'photo');

        return $retLoad;

    }

    // Delete: added files deletion
    public function afterDelete()
    {
        parent::afterDelete();

        // When delete the model, files automatically will be deleted
        \sfmobile\fileUpload\FileUploadCore::deleteAll($this->id, 'articles', 'articles', 'photo');

    }

    // Save: after saved the model, also save the files
    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);

        // File upload
        \sfmobile\fileUpload\FileUploadCore::sync($this, 'photo', \Yii::$app->user->identity->id, $this->id, 'articles', 'articles', 'photo');

    }

 
public function rules()
{
    return [
        // ...

        // minimum 2 files and maximum 5 files
        ['photo', 'file', 'minFiles' => 2, 'maxFiles' => 5 ],
    ];
}

<div class="articles-form">

     $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); 


    public function actionCreate()
    {
        $model = new Articles();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }


        return $this->render('update', [
            'model' => $model,
        ]);
    }    


public function actionUpdateMultiple($ids)
{
    $models = \common\models\Articles::find()->andWhere(['id' => explode(',', $ids)])->all();

    foreach($models as $indexModel => $model)
    {
        $model->formTabularIndex = $indexModel;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }
    }

    return $this->render('update-multiple', [
        'models' => $models,
    ]);
}


class BannerHomeIndexForm extends \common\models\BannerHome
{
    public $fileInputIndexName = null; // Extra parameter to specify which $_FILES index to use
    public $formTabularIndex = null; // Needed for file upload with tabular form
    public $image;

    // Load: initialize the files attached to model
    public function load($data, $formName = null)
    {
        $retLoad = parent::load($data, $formName);

        // !!! ATTENTION TO LAST PARAMETER !!!!
        \sfmobile\fileUpload\FileUploadCore::load($this->formTabularIndex, $this, 'image', $this->id, 'banner_home', 'banner_home', 'image', $this->fileInputIndexName);

        return $retLoad;

    }

    // Delete: added files deletion
    public function afterDelete()
    {
        // ... as default example ...
    }

    // Save: after saved the model, also save the files
    public function afterSave($insert, $changedAttributes)
    {
        // ... as default example ...
    }


public function actionIndex()
{
    if(\Yii::$app->request->isPost)
    {
        foreach($_FILES['BannerHomeForm']['name']['image'] as $indexBanner => $n)
        {
            $model = new BannerHomeForm();

            $model->formTabularIndex = $indexBanner;
            $model->fileInputIndexName = 'BannerHomeForm[image]['.$indexBanner.']';
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
            }
        }
    }
    \sfmobile\fileUpload\FileUploadCore::destroySession();

php composer.phar 

"fabriziocaldarelli/yii2-file-upload": "*"
@param $options array of [ 'filter' => [  'andWhere' => [] ], 'saveFields' => [ 'array of fields' ] ]