PHP Класс 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}', ], ]; }
Автор: Alexander Mohorev ([email protected])
Автор: Alexey Samoylov ([email protected])
Наследование: extends yii\base\Behavior
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$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

Открытые методы

Метод Описание
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.

Защищенные методы

Метод Описание
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.

Описание методов

afterDelete() публичный Метод

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

afterSave() публичный Метод

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

afterUpload() защищенный Метод

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() публичный Метод

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

beforeValidate() публичный Метод

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

delete() защищенный Метод

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

events() публичный Метод

public events ( )

generateFileName() защищенный Метод

Generates random filename.
protected generateFileName ( UploadedFile $file ) : string
$file yii\web\UploadedFile
Результат string

getFileName() защищенный Метод

protected getFileName ( UploadedFile $file ) : string
$file yii\web\UploadedFile
Результат string

getUploadPath() публичный Метод

Returns file path for the attribute.
public getUploadPath ( string $attribute, boolean $old = false ) : string | null
$attribute string
$old boolean
Результат string | null the file path.

getUploadUrl() публичный Метод

Returns file url for the attribute.
public getUploadUrl ( string $attribute ) : string | null
$attribute string
Результат string | null

init() публичный Метод

public init ( )

resolvePath() защищенный Метод

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

sanitize() публичный статический Метод

#my* unsaf&file:name?".png
public static sanitize ( string $filename ) : boolean
$filename string the source filename to be "sanitized"
Результат boolean string the sanitized filename

save() защищенный Метод

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
Результат boolean true whether the file is saved successfully

Описание свойств

$attribute публичное свойство

the attribute which holds the attachment.
public $attribute

$deleteTempFile публичное свойство

whether to delete the temporary file after saving.
public bool $deleteTempFile
Результат boolean

$generateNewName публичное свойство

generate a new unique name for the file set true or anonymous function takes the old filename and returns a new name.
См. также: self::generateFileName()
public $generateNewName

$instanceByName публичное свойство

Getting file instance by name
public $instanceByName

$path публичное свойство

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

$scenarios публичное свойство

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

$unlinkOnDelete публичное свойство

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

$unlinkOnSave публичное свойство

If true current attribute file will be deleted
public $unlinkOnSave

$url публичное свойство

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