final class FileUpload

Provides access to individual files that have been uploaded by a client.

Constants

IMAGE_MIME_TYPES

Properties

string read-only $name
string read-only $sanitizedName
string read-only $untrustedFullPath
string|null read-only $contentType
int read-only $size
string read-only $temporaryFile
int read-only $error
bool read-only $ok
string|null read-only $contents

Methods

__construct(array|null $value)

No description

string
getName() deprecated

No description

string
getUntrustedName()

Returns the original file name as submitted by the browser. Do not trust the value returned by this method.

string
getSanitizedName()

Returns the sanitized file name. The resulting name contains only ASCII characters [a-zA-Z0-9.-].

string
getUntrustedFullPath()

Returns the original full path as submitted by the browser during directory upload. Do not trust the value returned by this method. A client could send a malicious directory structure with the intention to corrupt or hack your application.

string|null
getContentType()

Detects the MIME content type of the uploaded file based on its signature. Requires PHP extension fileinfo.

int
getSize()

Returns the path of the temporary location of the uploaded file.

string
getTemporaryFile()

Returns the path of the temporary location of the uploaded file.

string
__toString()

Returns the path of the temporary location of the uploaded file.

int
getError()

Returns the error code. It is be one of UPLOAD_ERR_XXX constants.

bool
isOk()

Returns true if the file was uploaded successfully.

bool
hasFile()

Returns true if the user has uploaded a file.

move(string $dest)

Moves an uploaded file to a new location. If the destination file already exists, it will be overwritten.

bool
isImage()

Returns true if the uploaded file is a JPEG, PNG, GIF, or WebP image.

toImage()

Loads an image.

array|null
getImageSize()

Returns a pair of [width, height] with dimensions of the uploaded image.

string|null
getImageFileExtension()

Returns image file extension based on detected content type (without dot).

string|null
getContents()

Returns the contents of the uploaded file. If the upload was not successful, it returns null.

Details

at line 47
__construct(array|null $value)

No description

Parameters

array|null $value

at line 67
string getName() deprecated

deprecated use getUntrustedName()

No description

Return Value

string

at line 77
string getUntrustedName()

Returns the original file name as submitted by the browser. Do not trust the value returned by this method.

A client could send a malicious filename with the intention to corrupt or hack your application.

Return Value

string

at line 88
string getSanitizedName()

Returns the sanitized file name. The resulting name contains only ASCII characters [a-zA-Z0-9.-].

If the name does not contain such characters, it returns 'unknown'. If the file is JPEG, PNG, GIF, or WebP image, it returns the correct file extension. Do not blindly trust the value returned by this method.

Return Value

string

at line 110
string getUntrustedFullPath()

Returns the original full path as submitted by the browser during directory upload. Do not trust the value returned by this method. A client could send a malicious directory structure with the intention to corrupt or hack your application.

The full path is only available in PHP 8.1 and above. In previous versions, this method returns the file name.

Return Value

string

at line 120
string|null getContentType()

Detects the MIME content type of the uploaded file based on its signature. Requires PHP extension fileinfo.

If the upload was not successful or the detection failed, it returns null.

Return Value

string|null

at line 133
int getSize()

Returns the path of the temporary location of the uploaded file.

Return Value

int

at line 142
string getTemporaryFile()

Returns the path of the temporary location of the uploaded file.

Return Value

string

at line 151
string __toString()

Returns the path of the temporary location of the uploaded file.

Return Value

string

at line 161
int getError()

Returns the error code. It is be one of UPLOAD_ERR_XXX constants.

at line 170
bool isOk()

Returns true if the file was uploaded successfully.

Return Value

bool

at line 179
bool hasFile()

Returns true if the user has uploaded a file.

Return Value

bool

at line 188
FileUpload move(string $dest)

Moves an uploaded file to a new location. If the destination file already exists, it will be overwritten.

Parameters

string $dest

Return Value

FileUpload

at line 210
bool isImage()

Returns true if the uploaded file is a JPEG, PNG, GIF, or WebP image.

Detection is based on its signature, the integrity of the file is not checked. Requires PHP extension fileinfo.

Return Value

bool

at line 220
Image toImage()

Loads an image.

Return Value

Image

Exceptions

ImageException

at line 229
array|null getImageSize()

Returns a pair of [width, height] with dimensions of the uploaded image.

Return Value

array|null

at line 240
string|null getImageFileExtension()

Returns image file extension based on detected content type (without dot).

Return Value

string|null

at line 251
string|null getContents()

Returns the contents of the uploaded file. If the upload was not successful, it returns null.

Return Value

string|null

Traits

Strict class for better experience.