Documentation

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
author

Josh Lockhart info@joshlockhart.com

since
1.0.0

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

$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
throws
UploadException

If file uploads are disabled in the php.ini file

throws
InvalidArgumentException

If $_FILES key does not exist

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
string

getMd5()

Get md5

public getMd5() : string
Return values
string

getMimetype()

Get mimetype

public getMimetype() : string
Return values
string

getName()

Get name

public getName() : string
Return values
string

getNameWithExtension()

Get file name with extension

public getNameWithExtension() : string
Return values
string

getValidations()

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
int

isUploadedFile()

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
bool

setName()

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
throws
UploadException

If file does not validate

Return values
bool

validate()

Validate file

public validate() : bool
Return values
bool

True if valid, false if invalid

Loading…
On this page

Search results