2017 © Pedro Peláez
 

yii2-extension yii2-behavior-upload

File Upload Behavior

image

suver/yii2-behavior-upload

File Upload Behavior

  • Friday, July 27, 2018
  • by suver
  • Repository
  • 2 Watchers
  • 1 Stars
  • 48 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 12 Versions
  • 33 % Grown

The README.md

Frapse File Upload Behavior

File Upload Behavior, (*1)

Installation


php composer.phar require --prefer-dist yiisoft/yii2-imagine

Install migrations, (*2)

yii migrate --migrationPath=@vendor/suver/yii2-behavior-upload/migrations

How USE

Create @storage alias for you structur, (*3)

Configure you nginx server like this, (*4)

server {
    charset utf-8;
    client_max_body_size 128M;

    listen 80; ## listen for ipv4

    server_name storage.example.com;
    root        /var/www/com/example/storage/;
    index       index.php;

    location / {

    }   
}

You must add behavior like this, (*5)


public function behaviors() { return [ [ 'class' => TimestampBehavior::className(), 'value' => new Expression('NOW()'), ], [ 'class' => \suver\behavior\upload\UploadBehavior::className(), 'attribute' => 'photo', 'thumbnail' => [ 'admin_preview' => ['size' => '200x200', 'prefix' => 'v1'], 'admin_preview_without_animate' => ['size' => '100x100', 'prefix' => 'v2', 'option' => ['jpeg_quality' => 10], 'animate' => false], 'medium2' => ['size' => 'x100'], 'medium3' => ['size' => '100'], ], ], ]; }

You must add rule for file like this, (*6)


/** * @inheritdoc */ public function rules() { return [ ['photo', 'file', 'extensions' => ['jpg','png','gif'], 'maxSize' => 10*1024*1024, 'maxFiles' => 1] ]; }

Upgrade your upload form like this, (*7)

php, (*8)


<img src="<?php echo $model->linkedFile('photo')->thumbnail('admin_preview')->byDefault('/images/gogol.jpg') ?>">


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

Its all of you need create for uploaded files for you model ), (*9)

You don`t create data base or uploaded logic. All you need it add behafior and smoke sigars )), (*10)

REMEMBER: Your attribute for file from UploadBehavior options mast not exists in your model class, (*11)

OPTIONS

  • attribute - your file attribute
  • thumbnail - configuration for you thumbnail
    • size - size image like this 200x150 where 200 is width and 150 is height
    • prefix - add prefix for file name. When you modifien your thumbnail options you sey thenks for me fo this options ))
    • option - imagine option for save file
    • preGenerationOff - if this option is FALSE, this thumbnail don`t generated toafter uploaded file
  • animate - if animate == false you gif animation is off
  • instanceByName - if true your UploadedFile instance changed on UploadedFile::getInstancesByName
  • type - type of file. Exmaple: image. All type see UploadBehavior::$types
  • fileModel - You model for file modified. You class must be instanceof FileInterface like ImageFile class
  • messageFileNotFound - if file not found you see this message
  • multiUpload - if TRUE you can a lot uploaded file, else you can only one file

WARNING: If you wont to lot upload you must add multiUpload=true to your behavior options, (*12)

USE after upload

Like Example, (*13)


<img src="<?php echo $model->linkedFile('photo')->thumbnail('admin_preview')->byDefault('/images/gogol.jpg') ?>">

Other method, (*14)


$model->addFile(/var/www/com/example/storage/er/rt/errtsjdhfjsdhsdfsdfsd/errtsjdhfjsdhsdfsdfsd.jpg); // Add file from path $model->addHttpFile(http://storage.example.com/er/rt/errtsjdhfjsdhsdfsdfsd/errtsjdhfjsdhsdfsdfsd.jpg); // Add file from http $model->httpFileExists(http://storage.example.com/er/rt/errtsjdhfjsdhsdfsdfsd/errtsjdhfjsdhsdfsdfsd.jpg); // return TRUE if remote file is exists else FALSE var_dump($model->linkedFiles('photo')); //=> [ // ImageFile object, // File object, // ImageFile object, // ] // return list of uploaded file object echo $model->linkedFile('photo') // if exists you see http://storage.example.com/er/rt/errtsjdhfjsdhsdfsdfsd/errtsjdhfjsdhsdfsdfsd.jpg // if NOT exists you see '' $model->linkedFile('photo')->thumbnail('admin_preview') // if exists you see http://storage.example.com/er/rt/errtsjdhfjsdhsdfsdfsd/v1_errtsjdhfjsdhsdfsdfsd_200x200.jpg // if NOT exists you see '' $model->linkedFile('photo')->thumbnail('admin_preview')->byDefault('/images/gogol.jpg') // if exists you see http://storage.example.com/er/rt/errtsjdhfjsdhsdfsdfsd/v1_errtsjdhfjsdhsdfsdfsd_200x200.jpg // if NOT exists you see /images/gogol.jpg $model->linkedFile('photo')->getName(); // => errtsjdhfjsdhsdfsdfsd $model->linkedFile('photo')->getExtension(); // => jpg $model->linkedFile('photo')->delete(); // => delete you file $model->linkedFile('photo')->getPath(); // => /er/rt/errtsjdhfjsdhsdfsdfsd/errtsjdhfjsdhsdfsdfsd.jpg $model->linkedFile('photo')->getFullPath(); // => /var/www/com/example/storage/er/rt/errtsjdhfjsdhsdfsdfsd/errtsjdhfjsdhsdfsdfsd.jpg $model->linkedFile('photo')->getDomainPath(); // => http://storage.example.com/er/rt/errtsjdhfjsdhsdfsdfsd/errtsjdhfjsdhsdfsdfsd.jpg $model->linkedFile('photo')->hasFile(); // => TRUE if file exists else FALSE $model->linkedFile('photo')->getType(); // => UploadBehavior::TYPE_IMAGE == 1 $model->linkedFile('photo')->getMimeType(); // => image/jpeg $model->linkedFile('photo')->getSize(); // => 1545454 - image size $model->linkedFile('photo')->getOriginalName(); // => avatar.jpg $model->linkedFile('photo')->getDirectory() // => /er/rt/errtsjdhfjsdhsdfsdfsd $model->linkedFile('photo')->getParams(); // => [width => 1000, height => 1500] $model->linkedFile('photo')->getHash(); // => sdfsdfsfsdfsdfhsdjfh**sdfsdf - MD5 hash for file content

The Versions

27/07 2018

dev-master

9999999-dev

File Upload Behavior

  Sources   Download

Apache-2.0

The Requires

 

extension yii2 upload behavior farpse

27/07 2018

1.0.14

1.0.14.0

File Upload Behavior

  Sources   Download

Apache-2.0

The Requires

 

extension yii2 upload behavior farpse

26/07 2018

1.0.13

1.0.13.0

File Upload Behavior

  Sources   Download

Apache-2.0

The Requires

 

extension yii2 upload behavior farpse

30/06 2018

1.0.12

1.0.12.0

File Upload Behavior

  Sources   Download

Apache-2.0

The Requires

 

extension yii2 upload behavior farpse

15/03 2017

1.0.11

1.0.11.0

File Upload Behavior

  Sources   Download

Apache-2.0

The Requires

 

extension yii2 upload behavior farpse

07/03 2017

1.0.10

1.0.10.0

File Upload Behavior

  Sources   Download

Apache-2.0

The Requires

 

extension yii2 upload behavior farpse

23/10 2016

1.0.9

1.0.9.0

File Upload Behavior

  Sources   Download

Apache-2.0

The Requires

 

extension yii2 upload behavior farpse

18/10 2016

1.0.8

1.0.8.0

File Upload Behavior

  Sources   Download

Apache-2.0

The Requires

 

extension yii2 upload behavior farpse

17/10 2016

1.0.7

1.0.7.0

File Upload Behavior

  Sources   Download

Apache-2.0

The Requires

 

extension yii2 upload behavior farpse

10/10 2016

1.0.6

1.0.6.0

File Upload Behavior

  Sources   Download

Apache-2.0

The Requires

 

extension yii2 upload behavior farpse

08/10 2016

1.0.5

1.0.5.0

File Upload Behavior

  Sources   Download

Apache-2.0

The Requires

 

extension yii2 upload behavior farpse

08/10 2016

1.0.4

1.0.4.0

File Upload Behavior

  Sources   Download

Apache-2.0

The Requires

 

extension yii2 upload behavior farpse