Documentation

MediaUploadTrait

Implements media upload and delete functionality.

Table of Contents

Properties

$_upload_defaults  : array<string|int, mixed>

Methods

checkFileMetadata()  : string
Checks that file metadata meets the requirements. Returns new filename.
checkUploadedFile()  : string
Checks that uploaded file meets the requirements. Returns new filename.
copyUploadedFile()  : void
Copy uploaded file to the media collection.
createFromUploadedFile()  : Medium|null
Create Medium from an uploaded file.
deleteFile()  : void
Delete real file from the media collection.
getUploadSettings()  : array<string|int, mixed>
Get upload settings.
renameFile()  : void
Rename file inside the media collection.
clearCache()  : void
doAddUploadedMedium()  : void
doCopy()  : void
Internal logic to copy file.
doMoveUploadedFile()  : void
Internal logic to move uploaded file.
doRemove()  : void
Internal logic to remove file.
doRemoveMetadata()  : void
doRename()  : void
Internal logic to rename file.
doSanitizeSvg()  : void
doSaveMetadata()  : void
getConfig()  : Config
getGrav()  : Grav
getLanguage()  : Language
getPath()  : string|null
translate()  : string

Properties

$_upload_defaults

private array<string|int, mixed> $_upload_defaults = [ 'self' => true, // Whether path is in the media collection path itself. 'avoid_overwriting' => false, // Do not override existing files (adds datetime postfix if conflict). 'random_name' => false, // True if name needs to be randomized. 'accept' => ['image/*'], // Accepted mime types or file extensions. 'limit' => 10, // Maximum number of files. 'filesize' => null, // Maximum filesize in MB. 'destination' => null, ]

Methods

checkFileMetadata()

Checks that file metadata meets the requirements. Returns new filename.

public checkFileMetadata(array<string|int, mixed> $metadata[, string $filename = null ][, array<string|int, mixed>|null $settings = null ]) : string
Parameters
$metadata : array<string|int, mixed>
$filename : string = null
$settings : array<string|int, mixed>|null = null
Tags
throws
RuntimeException
Return values
string

checkUploadedFile()

Checks that uploaded file meets the requirements. Returns new filename.

public checkUploadedFile(UploadedFileInterface $uploadedFile[, string|null $filename = null ][, array<string|int, mixed>|null $settings = null ]) : string
Parameters
$uploadedFile : UploadedFileInterface
$filename : string|null = null
$settings : array<string|int, mixed>|null = null
Tags
example

$filename = null; // Override filename if needed (ignored if randomizing filenames). $settings = ['destination' => 'user://pages/media']; // Settings from the form field. $filename = $media->checkUploadedFile($uploadedFile, $filename, $settings); $media->copyUploadedFile($uploadedFile, $filename);

throws
RuntimeException
Return values
string

copyUploadedFile()

Copy uploaded file to the media collection.

public copyUploadedFile(UploadedFileInterface $uploadedFile, string $filename[, array<string|int, mixed>|null $settings = null ]) : void

WARNING: Always check uploaded file before copying it!

Parameters
$uploadedFile : UploadedFileInterface
$filename : string
$settings : array<string|int, mixed>|null = null
Tags
example

$settings = ['destination' => 'user://pages/media']; // Settings from the form field. $filename = $media->checkUploadedFile($uploadedFile, $filename, $settings); $media->copyUploadedFile($uploadedFile, $filename, $settings);

throws
RuntimeException

createFromUploadedFile()

Create Medium from an uploaded file.

public createFromUploadedFile(UploadedFileInterface $uploadedFile[, array<string|int, mixed> $params = [] ]) : Medium|null
Parameters
$uploadedFile : UploadedFileInterface
$params : array<string|int, mixed> = []
Return values
Medium|null

deleteFile()

Delete real file from the media collection.

public deleteFile(string $filename[, array<string|int, mixed>|null $settings = null ]) : void
Parameters
$filename : string
$settings : array<string|int, mixed>|null = null
Tags
throws
RuntimeException

getUploadSettings()

Get upload settings.

public getUploadSettings([array<string|int, mixed>|null $settings = null ]) : array<string|int, mixed>
Parameters
$settings : array<string|int, mixed>|null = null

Form field specific settings (override).

Return values
array<string|int, mixed>

renameFile()

Rename file inside the media collection.

public renameFile(string $from, string $to[, array<string|int, mixed>|null $settings = null ]) : void
Parameters
$from : string
$to : string
$settings : array<string|int, mixed>|null = null

doAddUploadedMedium()

protected doAddUploadedMedium(string $name, string $filename, string $path) : void
Parameters
$name : string
$filename : string
$path : string

doCopy()

Internal logic to copy file.

protected doCopy(string $src, string $dst, string $path) : void
Parameters
$src : string
$dst : string
$path : string

doMoveUploadedFile()

Internal logic to move uploaded file.

protected doMoveUploadedFile(UploadedFileInterface $uploadedFile, string $filename, string $path) : void
Parameters
$uploadedFile : UploadedFileInterface
$filename : string
$path : string

doRemove()

Internal logic to remove file.

protected doRemove(string $filename, string $path) : void
Parameters
$filename : string
$path : string

doRemoveMetadata()

protected doRemoveMetadata(string $filename, string $path) : void
Parameters
$filename : string
$path : string

doRename()

Internal logic to rename file.

protected doRename(string $from, string $to, string $path) : void
Parameters
$from : string
$to : string
$path : string

doSanitizeSvg()

protected doSanitizeSvg(string $filename, string $path) : void
Parameters
$filename : string
$path : string

doSaveMetadata()

protected doSaveMetadata(array<string|int, mixed> $metadata, string $filename, string $path) : void
Parameters
$metadata : array<string|int, mixed>
$filename : string
$path : string

getPath()

protected abstract getPath() : string|null
Return values
string|null

translate()

protected translate(string $string) : string
Parameters
$string : string
Return values
string

        
On this page

Search results