Documentation

FileUpload
in package

Used for managing backend processing of fields which have had data submitted via chunked file uploads, i.e. where chunked upload data has been stitched together by {@see FileUploadController}

Table of Contents

Methods

checkFilename()  : bool
Check a given filename is allowed to be uploaded - blocks PHP files etc
generateFromDisk()  : string
Generates a fake upload, including a session entry and a temp symlink, from an existing file on disk.
getErrorMessage()  : string
Get a human friendly error string from the `$_FILES[*]['error]` value.
getFile()  : array<string|int, mixed>|null
Get a single file object from the $_FILES superglobal with a query string.
verify()  : string
Verify that the temporary file specified by the user exists and has valid content, and that the original file name is acceptable This is to be used during form processing upon a POST submission.

Methods

checkFilename()

Check a given filename is allowed to be uploaded - blocks PHP files etc

public static checkFilename(string $filename) : bool
Parameters
$filename : string
Return values
bool —

True if allowed, false if not

generateFromDisk()

Generates a fake upload, including a session entry and a temp symlink, from an existing file on disk.

public static generateFromDisk(string $filename, string $session_key, string $field_name) : string

This allows reuse of existing uploaded files on Form::chunkedUpload fields which support multiple files

Parameters
$filename : string

The name of a file which exists in the files dir, e.g. 'image.jpg'

$session_key : string

The session key used for file uploads, e.g. 'user-register' (see Fb::chunkedUpload)

$field_name : string

The name of the field supplied to Fb::chunkedUpload, e.g. 'photos'

Tags
throws
InvalidArgumentException

if the filename is invalid

Return values
string —

The filename of the newly generated symlink, which matches the naming format of files uploaded via the chunked uploader

getErrorMessage()

Get a human friendly error string from the `$_FILES[*]['error]` value.

public static getErrorMessage(int $error) : string
Parameters
$error : int
Return values
string

getFile()

Get a single file object from the $_FILES superglobal with a query string.

public static getFile(string $key) : array<string|int, mixed>|null
Parameters
$key : string

like multiedit_items.0.image

Return values
array<string|int, mixed>|null —

null if the key doesn't exist, otherwise:

  • name
  • type
  • tmp_name
  • error
  • size

verify()

Verify that the temporary file specified by the user exists and has valid content, and that the original file name is acceptable This is to be used during form processing upon a POST submission.

public static verify(string $sess_key, string $field, int $index, array<string|int, mixed> $allowed_exts) : string
Parameters
$sess_key : string

Session key related to the form, e.g. 'user-register'; see $params['sess_key'] of Fb::chunkedUpload

$field : string

Name of field to process, e.g. 'image'

$index : int

Array index; 0 for the first file, 1 for the next and so on

$allowed_exts : array<string|int, mixed>

Array of string file-types that are allowed

Tags
example

// Expects $_POST['avatar'][0] and $_POST['avatar_temp'][0] to both be set // Expects $_SESSION['file_uploads']['user_details']['avatar']['code'] to be set $result = FileUpload::verify('user_details', 'avatar', 0, ['jpg', 'gif', 'png']); // Result will be something like '/home/.../storage/temp/xxxx'

post

string $field

string $field . '_temp'

throws
DomainException

If the $_POST upload state is invalid.

FileUploadException

If there was an issue the uploader should know about, e.g. the file extension isn't permitted. This exception always has front-end safe exception messages.

Return values
string —

Path to temporarily uploaded file; this can be used as the first argument to File::moveUpload to put the file in the desired permanent location.

On this page

Search results