PHP Class mongosoft\file\UploadBehavior

To use UploadBehavior, insert the following code to your ActiveRecord class: php use mongosoft\file\UploadBehavior; function behaviors() { return [ [ 'class' => UploadBehavior::className(), 'attribute' => 'file', 'scenarios' => ['insert', 'update'], 'path' => '@webroot/upload/{id}', 'url' => '@web/upload/{id}', ], ]; }
Author: Alexander Mohorev ([email protected])
Author: Alexey Samoylov ([email protected])
Inheritance: extends yii\base\Behavior
Show file Open project: mongosoft/yii2-upload-behavior Class Usage Examples

Public Properties

Property Type Description
$attribute the attribute which holds the attachment.
$deleteTempFile boolean whether to delete the temporary file after saving.
$generateNewName generate a new unique name for the file set true or anonymous function takes the old filename and returns a new name.
$instanceByName Getting file instance by name
$path the base path or path alias to the directory in which to save files.
$scenarios the scenarios in which the behavior will be triggered
$unlinkOnDelete If true current attribute file will be deleted after model deletion.
$unlinkOnSave If true current attribute file will be deleted
$url the base URL or path alias for this file

Public Methods

Method Description
afterDelete ( ) This method is invoked after deleting a record.
afterSave ( ) This method is called at the end of inserting or updating a record.
beforeSave ( ) This method is called at the beginning of inserting or updating a record.
beforeValidate ( ) This method is invoked before validation starts.
events ( )
getUploadPath ( string $attribute, boolean $old = false ) : string | null Returns file path for the attribute.
getUploadUrl ( string $attribute ) : string | null Returns file url for the attribute.
init ( )
sanitize ( string $filename ) : boolean Replaces characters in strings that are illegal/unsafe for filename.

Protected Methods

Method Description
afterUpload ( ) This method is invoked after uploading a file.
delete ( string $attribute, boolean $old = false ) Deletes old file.
generateFileName ( UploadedFile $file ) : string Generates random filename.
getFileName ( UploadedFile $file ) : string
resolvePath ( $path ) Replaces all placeholders in path variable with corresponding values.
save ( UploadedFile $file, string $path ) : boolean Saves the uploaded file.

Method Details

afterDelete() public method

This method is invoked after deleting a record.
public afterDelete ( )

afterSave() public method

This method is called at the end of inserting or updating a record.
public afterSave ( )

afterUpload() protected method

The default implementation raises the [[EVENT_AFTER_UPLOAD]] event. You may override this method to do postprocessing after the file is uploaded. Make sure you call the parent implementation so that the event is raised properly.
protected afterUpload ( )

beforeSave() public method

This method is called at the beginning of inserting or updating a record.
public beforeSave ( )

beforeValidate() public method

This method is invoked before validation starts.
public beforeValidate ( )

delete() protected method

Deletes old file.
protected delete ( string $attribute, boolean $old = false )
$attribute string
$old boolean

events() public method

public events ( )

generateFileName() protected method

Generates random filename.
protected generateFileName ( UploadedFile $file ) : string
$file yii\web\UploadedFile
return string

getFileName() protected method

protected getFileName ( UploadedFile $file ) : string
$file yii\web\UploadedFile
return string

getUploadPath() public method

Returns file path for the attribute.
public getUploadPath ( string $attribute, boolean $old = false ) : string | null
$attribute string
$old boolean
return string | null the file path.

getUploadUrl() public method

Returns file url for the attribute.
public getUploadUrl ( string $attribute ) : string | null
$attribute string
return string | null

init() public method

public init ( )

resolvePath() protected method

Replaces all placeholders in path variable with corresponding values.
protected resolvePath ( $path )

sanitize() public static method

#my* unsaf&file:name?".png
public static sanitize ( string $filename ) : boolean
$filename string the source filename to be "sanitized"
return boolean string the sanitized filename

save() protected method

Saves the uploaded file.
protected save ( UploadedFile $file, string $path ) : boolean
$file yii\web\UploadedFile the uploaded file instance
$path string the file path used to save the uploaded file
return boolean true whether the file is saved successfully

Property Details

$attribute public property

the attribute which holds the attachment.
public $attribute

$deleteTempFile public property

whether to delete the temporary file after saving.
public bool $deleteTempFile
return boolean

$generateNewName public property

generate a new unique name for the file set true or anonymous function takes the old filename and returns a new name.
See also: self::generateFileName()
public $generateNewName

$instanceByName public property

Getting file instance by name
public $instanceByName

$path public property

the base path or path alias to the directory in which to save files.
public $path

$scenarios public property

the scenarios in which the behavior will be triggered
public $scenarios

$unlinkOnDelete public property

If true current attribute file will be deleted after model deletion.
public $unlinkOnDelete

$unlinkOnSave public property

If true current attribute file will be deleted
public $unlinkOnSave

$url public property

the base URL or path alias for this file
public $url