File
extends SplFileInfo
in package
File
This class provides the implementation for an uploaded file. It exposes common attributes for the uploaded file (e.g. name, extension, media type) and allows you to attach validations to the file that must pass for the upload to succeed.
Tags
Table of Contents
Properties
- $extension : string
- File extension (without leading dot)
- $errorCode : int
- Upload error code (for internal use only)
- $errorCodeMessages : array<string|int, mixed>
- Upload error code messages
- $errors : array<string|int, mixed>
- Validation errors
- $mimetype : string
- File mimetype (e.g. "image/png")
- $name : string
- File name (without extension)
- $originalName : string
- Original file name provided by client (for internal use only)
- $storage : Base
- Storage delegate
- $units : array<string|int, mixed>
- Lookup hash to convert file units to bytes
- $validations : mixed
- Validations
Methods
- __construct() : mixed
- Constructor
- addError() : File
- Add file validation error
- addValidations() : mixed
- Add file validations
- getDimensions() : array<string|int, mixed>
- Get image dimensions
- getErrors() : mixed
- Get file validation errors
- getExtension() : string
- Get file extension (without leading dot)
- getMd5() : string
- Get md5
- getMimetype() : string
- Get mimetype
- getName() : string
- Get name
- getNameWithExtension() : string
- Get file name with extension
- getValidations() : mixed
- Get file validations
- humanReadableToBytes() : int
- Convert human readable file size (e.g. "10K" or "3M") into bytes
- isOk() : bool
- Is this file OK?
- isUploadedFile() : bool
- Is this file uploaded with a POST request?
- setName() : File
- Set name (without extension)
- upload() : bool
- Upload file (delegated to storage object)
- validate() : bool
- Validate file
Properties
$extension
File extension (without leading dot)
public
string
$extension
$errorCode
Upload error code (for internal use only)
protected
int
$errorCode
Tags
$errorCodeMessages
Upload error code messages
protected
static array<string|int, mixed>
$errorCodeMessages
= array(1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini', 2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form', 3 => 'The uploaded file was only partially uploaded', 4 => 'No file was uploaded', 6 => 'Missing a temporary folder', 7 => 'Failed to write file to disk', 8 => 'A PHP extension stopped the file upload')
$errors
Validation errors
protected
array<string|int, mixed>
$errors
$mimetype
File mimetype (e.g. "image/png")
protected
string
$mimetype
$name
File name (without extension)
protected
string
$name
$originalName
Original file name provided by client (for internal use only)
protected
string
$originalName
$storage
Storage delegate
protected
Base
$storage
$units
Lookup hash to convert file units to bytes
protected
static array<string|int, mixed>
$units
= array('b' => 1, 'k' => 1024, 'm' => 1048576, 'g' => 1073741824)
$validations
Validations
protected
mixed
$validations
Methods
__construct()
Constructor
public
__construct(string $key, Base $storage) : mixed
Parameters
- $key : string
-
The file's key in $_FILES superglobal
- $storage : Base
-
The method with which to store file
Tags
addError()
Add file validation error
public
addError(mixed $error) : File
Parameters
- $error : mixed
Return values
File —Self
addValidations()
Add file validations
public
addValidations(Base|mixed $validations) : mixed
Parameters
- $validations : Base|mixed
getDimensions()
Get image dimensions
public
getDimensions() : array<string|int, mixed>
Return values
array<string|int, mixed> —formatted array of dimensions
getErrors()
Get file validation errors
public
getErrors() : mixed
getExtension()
Get file extension (without leading dot)
public
getExtension() : string
Return values
stringgetMd5()
Get md5
public
getMd5() : string
Return values
stringgetMimetype()
Get mimetype
public
getMimetype() : string
Return values
stringgetName()
Get name
public
getName() : string
Return values
stringgetNameWithExtension()
Get file name with extension
public
getNameWithExtension() : string
Return values
stringgetValidations()
Get file validations
public
getValidations() : mixed
humanReadableToBytes()
Convert human readable file size (e.g. "10K" or "3M") into bytes
public
static humanReadableToBytes(string $input) : int
Parameters
- $input : string
Return values
intisOk()
Is this file OK?
public
isOk() : bool
This method inspects the upload error code to see if the upload was successful or if it failed for a variety of reasons.
Tags
Return values
boolisUploadedFile()
Is this file uploaded with a POST request?
public
isUploadedFile() : bool
This is a separate method so that it can be stubbed in unit tests to avoid
the hard dependency on the is_uploaded_file
function.
Return values
boolsetName()
Set name (without extension)
public
setName(string $name) : File
Parameters
- $name : string
Return values
File —Self
upload()
Upload file (delegated to storage object)
public
upload([string $newName = null ]) : bool
Parameters
- $newName : string = null
-
Give the file it a new name
Tags
Return values
boolvalidate()
Validate file
public
validate() : bool
Return values
bool —True if valid, false if invalid