class Uploads

Provides support for file uploads.

Constants

MULTIPLE

Constant for specifying multiple file upload.

SINGLE

Constant for specifying single file upload.

Properties

private string $_bucket

Path to the bucket where file are stored.

protected array $_allowedFileTypes

An array of allowed file types.

private array $_errors

An array of error objects.

protected string $_fieldName

The name of the field associated with file upload.

protected array $_files

An array containing objects with information about files.

protected int $_maxAllowedSize

The maximum allowed upload size.

protected string $sizeMsg

Message describing maximum allowable size.

Methods

__construct(array|string $files, array $fileTypes, int $maxAllowedSize, string $bucket, string $sizeMsg, string $mode)

Creates instance of Uploads class.

void
addErrorMessage(string $name, string $message)

Adds an error message to the $_errors array.

void
errorReporting(bool|array $errors, Model $model, string $name)

Processes list of errors associated with uploads and makes them presentable to user during validation.

string
generateUploadFilename(string $originalFilename)

Generates a unique filename for an uploaded file while preserving its extension.

array
getFiles()

Getter function for the $_files array.

static Uploads|null
handleUpload(array $file, string $uploadModel, string $bucket, string $sizeMsg, Model $model, string $name, string $mode = self::SINGLE)

Handles file uploads and returns an Uploads instance if valid.

static array
restructureFiles(array $files, string $mode = self::SINGLE)

Restructures $_FILES data based on mode.

void
runValidation()

Performs validation tasks.

void
upload(string $path, string $uploadName, string $fileName)

Performs file upload.

bool|array
validates()

Reports on success of validation.

void
validateFileType()

Validates file type and sets error message if file type is invalid.

void
validateSize()

Validates file size and sets error message if file is too large.

Details

at line 72
__construct(array|string $files, array $fileTypes, int $maxAllowedSize, string $bucket, string $sizeMsg, string $mode)

Creates instance of Uploads class.

Parameters

array|string $files

Array of files to be uploaded.

array $fileTypes
int $maxAllowedSize

Maximum allowable size for a particular file. This can vary depending on requirements.

string $bucket

The location where the files will be stored.

string $sizeMsg

The message describing the maximum allowable size usually described as <bytes|mb|gb> (e.g.: 5mb).

string $mode

A constant to set whether or not we are working with a single file upload or an array regarding form setup.

at line 98
protected void addErrorMessage(string $name, string $message)

Adds an error message to the $_errors array.

Parameters

string $name

The name of the error.

string $message

The message associated with this error.

Return Value

void

at line 115
void errorReporting(bool|array $errors, Model $model, string $name)

Processes list of errors associated with uploads and makes them presentable to user during validation.

Parameters

bool|array $errors

The errors, if any are detected will be an array.

Model $model

The model associated with the errors.

string $name

The name of the field in the model for the errors.

Return Value

void

at line 140
string generateUploadFilename(string $originalFilename)

Generates a unique filename for an uploaded file while preserving its extension.

Uses a cryptographically secure random hash to create a unique base name. Falls back to 'bin' if no extension is found in the original filename.

Parameters

string $originalFilename

The original filename (used to extract the extension).

Return Value

string

A unique, safely generated filename with the original extension.

Exceptions

Exception

at line 153
array getFiles()

Getter function for the $_files array.

Return Value

array

The $_files array.

at line 172
static Uploads|null handleUpload(array $file, string $uploadModel, string $bucket, string $sizeMsg, Model $model, string $name, string $mode = self::SINGLE)

Handles file uploads and returns an Uploads instance if valid.

Parameters

array $file

The file input from $_FILES.

string $uploadModel

The name of the model class responsible for uploads.

string $bucket

Upload destination.

string $sizeMsg

Size description for error messages.

Model $model

The associated with the view you are working with. May or may not be same as $uploadModel if $uploadModel has index id field associated with another model.

string $name

The name of the field for upload from form.

string $mode

Use Uploads::SINGLE for single file uploads or Uploads::MULTIPLE for multiple file uploads.

Return Value

Uploads|null

Returns Uploads instance if valid, otherwise null.

at line 218
static array restructureFiles(array $files, string $mode = self::SINGLE)

Restructures $_FILES data based on mode.

Parameters

array $files

The uploaded file(s).

string $mode

Upload mode: Uploads::SINGLE or Uploads::MULTIPLE.

Return Value

array

Structured file array.

at line 250
void runValidation()

Performs validation tasks.

Return Value

void

at line 286
void upload(string $path, string $uploadName, string $fileName)

Performs file upload.

Parameters

string $path

Directory where file will exist when uploaded.

string $uploadName

The actual name for the file when uploaded.

string $fileName

The temporary file name.

Return Value

void

at line 306
bool|array validates()

Reports on success of validation.

Return Value

bool|array

True if validation is successful. Otherwise, we return the $_errors array.

at line 315
protected void validateFileType()

Validates file type and sets error message if file type is invalid.

Return Value

void

at line 355
protected void validateSize()

Validates file size and sets error message if file is too large.

Return Value

void