PHP Classes

Yii2 PHP Active Record Uploader Behavior: Process file uploads to use with model classes

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-03-25 (6 months ago) RSS 2.0 feedNot enough user ratingsTotal: 15 All time: 11,365 This week: 73Up
Version License PHP version Categories
yii2-uploader-behavi 1.0.0BSD License5PHP 5, Databases, Data types, Design ...
Description 

Author

This package can process file uploads to use with model classes.

It provides a behaviors class to process uploaded files stored using model classes provided by the Yii2 framework.

The behavior class registers event handlers that alter how model class objects are stored and retrieved to process model object properties with associated file data.

That class can process the uploaded files using forms before storing the data of the uploaded file in the model class object.

Innovation Award
PHP Programming Innovation award nominee
March 2024
Number 8
Active Record is a well-known software design pattern used to store and retrieve model objects, for instance, from SQL database tables.

Usually, model class objects have properties with small-sized types.

To store data that can be larger, it is better to use files or large object (LOB) database files.

This package implements a behavior class that can process file uploads for model class properties of large sizes.

Manuel Lemos
Picture of Gabriel Alejandro López López
  Performance   Level  
Name: Gabriel Alejandro López ... <contact>
Classes: 10 packages by
Country: United States United States
Age: 35
All time rank: 4332551 in United States United States
Week rank: 18 Up4 in United States United States Up
Innovation award
Innovation award
Nominee: 3x

Documentation

Yii2 Uploader Behavior for Active Record

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

Automates file uploading for every attribute tagged with file or image validation rule. It also configures the way that filename should be renamed and stored in database as attribute value.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist daxslab/yii2-uploader-behavior "*"

or add

"daxslab/yii2-uploader-behavior": "*"

to the require section of your composer.json file.

Usage

Once the extension is installed, simply use it in your models by :

use daxslab/behaviors/UploaderBehavior;

public function behaviors() {
    return [
        UploaderBehavior::className()
    ];
}

This is the simplest way of usage. It will look for all attributes with a file or image validation rule.

public function rules()
{
    return [
        [['name'], 'required'],
        [['name'], 'string', 'max' => 256],
        [['image'], 'image', 'skipOnEmpty' => true],
    ];
}

And update the form to specify a file input for the image attribute:

$form->field($model, 'image')->fileInput();

For a more controlled usage, you can specify which attributes to control using the attributes option as in :

use daxslab/behaviors/UploaderBehavior;

public function behaviors() {
    return [
        [
            'class' => UploaderBehavior::className(),
            'attributes' => ['avatar'] // or you can use the string format as in 'attributes' => 'avatar'
        ]
    ];
}

By default the filename of the uploaded file is randomized, but if you want to configure it you can setup the renamer option:

use daxslab/behaviors/UploaderBehavior;

public function behaviors() {
    return [
        [
            'class' => UploaderBehavior::className(),
            'attributes' => ['avatar'] // or you can use the string format as in 'attributes' => 'avatar'
            'renamer' => UploaderBehavior::RENAME_MD5 //will encode the filename with md5()
        ]
    ];
}

also you can specify a custom function to rename the filename:

use daxslab/behaviors/UploaderBehavior;

public function behaviors() {
    return [
        [
            'class' => UploaderBehavior::className(),
            'attributes' => ['avatar'] // or you can use the string format as in 'attributes' => 'avatar'
            'renamer' => function($name, $owner){
                return strtoupper($name); //will turn the filename into uppercase
            }
        ]
    ];
}

Proudly made by Daxslab.


  Files folder image Files (5)  
File Role Description
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE.md Lic. License text
Accessible without login Plain text file README.md Doc. Documentation
Plain text file UploaderBehavior.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:15
This week:0
All time:11,365
This week:73Up