2017 © Pedro Peláez
 

yii2-components yii2-filestorage

Yii2 component and models for storage uploaded files

image

sem-soft/yii2-filestorage

Yii2 component and models for storage uploaded files

  • Friday, February 2, 2018
  • by sem
  • Repository
  • 1 Watchers
  • 1 Stars
  • 62 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 14 Versions
  • 0 % Grown

The README.md

Yii2 component and models for storage uploaded files

Install by composer

composer require sem-soft/yii2-filestorage, (*1)

Or add this code into require section of your composer.json and then call composer update in console

"sem-soft/yii2-filestorage": "*", (*2)

Execute migration

$ ./yii migrate/up --migrationPath=@vendor/sem-soft/yii2-filestorage/migrations

Usage

In configuration file do, (*3)

<?php
...
  'components'  =>  [
    ...
    'filestorage'   =>  [
        'class'             => \sem\filestorage\FileStorage::className(),
        'storageBaseUrl'    =>  false,
        'storagePath'       =>  '@webroot',
        'storageDir'        =>  'upload',
        'filemode'          =>  0775 // Если задан, то после создания файла принудительно будет произведена смена прав на указанные
    ]
    ...
  ],
...
 ?>
 ```
Example of Controller action for file uploading
```php
    public function actionIndex()
    {
    $model = new \backend\models\FileForm();

    if (Yii::$app->request->isPost) {

            $model->imageFile = UploadedFile::getInstance($model, 'imageFile');

            $file = $model->upload();

        }

        return $this->render('index', [
        'model' =>  $model
    ]);
    }
 ```
 ```php
     public function actionIndex1()
    {
        $model = new \backend\models\FileForm();


            $model->imageFile = new \sem\filestorage\adapters\RemoteFile("https://cs7065.userapi.com/c836722/v836722161/4bff2/mdg7cPZvLrM.jpg");

            $file = $model->upload();

        return $this->render('index', [
                'model' => $model
        ]);
    }
 ```
Example of Upload From Model
```php
 false, 'extensions' => 'png, jpg'],
        ];
    }
    
    /**
     * @return boolean
     */
    public function upload()
    {
        if ($this->validate()) {
        $file = new \sem\filestorage\models\File($this->imageFile,[
        'group_code'    =>  'banners',
        'object_id' =>  '345',
        'allowedExtensions' =>  [
            'png',
            'jpeg',
            'jpg'
        ]
        ]);
        if ($file->save()) {
        return $file;
        }
        }
    
    return false;
    }
}
```
Example of Form View
```php
     ['enctype' => 'multipart/form-data']]) ?>

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

        <button type="submit">Submit</button>
    <?php ActiveForm::end() ?>

Example of displaying files data, (*4)

    public function actionTest()
    {
    foreach (\sem\filestorage\models\File::find()->all() as $f) {
        echo $f->getUrl(true) . "<br>";
        echo $f->url . "<br>";
        echo $f->name . "<br>";
        echo $f->path . "<br>";
        echo $f->size . "<br>";
        echo \sem\helpers\FileHelper::formatSize($f->size) . "<br>";
        echo "<br>";
        echo "<br>";
    }
    }

Example of displaying images data and cache, (*5)

    public function actionTest()
    {
        foreach (\sem\filestorage\models\Image::find()->all() as $f) {
            echo $f->getUrl(true) . "<br>";
            echo $f->url . "<br>";
            echo $f->name . "<br>";
            echo $f->path . "<br>";
            echo $f->size . "<br>";
            echo \sem\helpers\FileHelper::formatSize($f->size) . "<br>";
            echo $f->isImage . "<br>";

            echo "------heighten:<br>";
            $heighten = $f->heighten(300);
            echo $heighten->getUrl(true) . "<br>";
            echo $heighten->url . "<br>";
            echo $heighten->path . "<br>";
            echo \yii\helpers\Html::img($heighten->url);
            echo "<br>";

            echo "------widen:<br>";
            $widen = $f->widen(200);
            echo $widen->getUrl(true) . "<br>";
            echo $widen->url . "<br>";
            echo $widen->path . "<br>";
            echo \yii\helpers\Html::img($widen->url);
            echo "<br>";

            echo "------contain:<br>";
            $contain = $f->contain(100, 120);
            echo $contain->getUrl(true) . "<br>";
            echo $contain->url . "<br>";
            echo $contain->path . "<br>";
            echo \yii\helpers\Html::img($contain->url);
            echo "<br>";

            echo "------cover:<br>";
            $cover = $f->cover(100, 100);
            echo $cover->getUrl(true) . "<br>";
            echo $cover->url . "<br>";
            echo $cover->path . "<br>";
            echo \yii\helpers\Html::img($cover->url);
            echo "<br>";
            echo "<br>";
        }
    }

The Versions

02/02 2018

dev-master

9999999-dev

Yii2 component and models for storage uploaded files

  Sources   Download

BSD-3-Clause BSD 3-Clause

The Requires

 

by Samsonov Vladimir

02/02 2018

dev-add-license-1

dev-add-license-1

Yii2 component and models for storage uploaded files

  Sources   Download

BSD-3-Clause

The Requires

 

by Samsonov Vladimir

02/02 2018

1.0.10

1.0.10.0

Yii2 component and models for storage uploaded files

  Sources   Download

BSD-3-Clause

The Requires

 

by Samsonov Vladimir

02/02 2018

1.0.9.1

1.0.9.1

Yii2 component and models for storage uploaded files

  Sources   Download

BSD 3-Clause

The Requires

 

by Samsonov Vladimir

22/09 2017

1.0.9

1.0.9.0

Yii2 component and models for storage uploaded files

  Sources   Download

BSD 3-Clause

The Requires

 

by Samsonov Vladimir

07/07 2017

1.0.7

1.0.7.0

Yii2 component and models for storage uploaded files

  Sources   Download

BSD 3-Clause

The Requires

 

by Samsonov Vladimir

07/07 2017

1.0.8

1.0.8.0

Yii2 component and models for storage uploaded files

  Sources   Download

BSD 3-Clause

The Requires

 

by Samsonov Vladimir

05/07 2017

1.0.6

1.0.6.0

Yii2 component and models for storage uploaded files

  Sources   Download

BSD 3-Clause

The Requires

 

by Samsonov Vladimir

01/07 2017

1.0.5

1.0.5.0

Yii2 component and models for storage uploaded files

  Sources   Download

BSD 3-Clause

The Requires

 

by Samsonov Vladimir

30/06 2017

1.0.4

1.0.4.0

Yii2 component and models for storage uploaded files

  Sources   Download

BSD 3-Clause

The Requires

 

by Samsonov Vladimir

29/06 2017

1.0.3

1.0.3.0

Yii2 component and models for storage uploaded files

  Sources   Download

BSD 3-Clause

The Requires

 

by Samsonov Vladimir

29/06 2017

1.0.2

1.0.2.0

Yii2 component and models for storage uploaded files

  Sources   Download

BSD 3-Clause

The Requires

 

by Samsonov Vladimir

26/06 2017

1.0.1

1.0.1.0

Yii2 component and models for storage uploaded files

  Sources   Download

BSD 3-Clause

The Requires

 

by Samsonov Vladimir

22/06 2017

1.0.0

1.0.0.0

Yii2 component and models for storage uploaded files

  Sources   Download

BSD 3-Clause

The Requires

 

by Samsonov Vladimir