UploadedFile implements UploadedFileInterface

Class which represents one uploaded file, usually coming from $_FILES, according to PSR-7 standard.

Highly inspired by https://github.com/phly/http/

Internal

Note that this is not public API yet.

Table of Contents

Interfaces

UploadedFileInterface

Properties

$clientFilename  : string|null
$clientMediaType  : string|null
$error  : int
$file  : string|null
$moved  : bool
$size  : int
$stream  : StreamInterface|null

Methods

__construct()  : mixed
Constructor method
getClientFilename()  : string|null
Retrieve the filename sent by the client.
getClientMediaType()  : string|null
Retrieve the media type sent by the client.
getError()  : int
Retrieve the error associated with the uploaded file.
getSize()  : int|null
Retrieve the file size.
getStream()  : StreamInterface
Retrieve a stream representing the uploaded file.
getTemporaryFileName()  : string|null
Retrieve the temporary file name (for example /tmp/tmp_foo_filexyz If the file has been moved (by moveTo) an exception is thrown.
moveTo()  : void
Move the uploaded file to a new location.

Properties

$clientFilename

protected string|null $clientFilename

$clientMediaType

protected string|null $clientMediaType

$stream

protected StreamInterface|null $stream = null

Methods

__construct()

Constructor method

public __construct(string|resource|StreamInterface $input, int $size, int $errorStatus[, string|null $clientFilename = null ][, string|null $clientMediaType = null ]) : mixed
Parameters
$input : string|resource|StreamInterface

is either a stream or a filename

$size : int

see $_FILES['size'] from PHP

$errorStatus : int

see $_FILES['error']

$clientFilename : string|null = null

the original filename handed over from the client

$clientMediaType : string|null = null

the media type (optional)

Tags
throws
InvalidArgumentException

getClientFilename()

Retrieve the filename sent by the client.

public getClientFilename() : string|null

Usually returns the value stored in the "name" key of the file in the $_FILES array.

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 values
string|null

The filename sent by the client or null if none was provided.

getClientMediaType()

Retrieve the media type sent by the client.

public getClientMediaType() : string|null

Usually returns the value stored in the "type" key of the file in the $_FILES array.

Do not trust the value returned by this method. A client could send a malicious media type with the intention to corrupt or hack your application.

Return values
string|null

The media type sent by the client or null if none was provided.

getError()

Retrieve the error associated with the uploaded file.

public getError() : int

Usually returns the value stored in the "error" key of the file in the $_FILES array.

The return value MUST be one of PHP's UPLOAD_ERR_XXX constants.

If the file was uploaded successfully, this method MUST return UPLOAD_ERR_OK.

Tags
see
https://php.net/manual/en/features.file-upload.errors.php
Return values
int

One of PHP's UPLOAD_ERR_XXX constants.

getSize()

Retrieve the file size.

public getSize() : int|null

Usually returns the value stored in the "size" key of the file in the $_FILES array if available, as PHP calculates this based on the actual size transmitted.

Return values
int|null

The file size in bytes or null if unknown.

getStream()

Retrieve a stream representing the uploaded file.

public getStream() : StreamInterface

Returns a StreamInterface instance, representing the uploaded file. The purpose of this method is to allow utilizing native PHP stream functionality to manipulate the file upload, such as stream_copy_to_stream() (though the result will need to be decorated in a native PHP stream wrapper to work with such functions).

If the moveTo() method has been called previously, this method raises an exception.

Tags
throws
RuntimeException

in cases when no stream is available or can be created.

Return values
StreamInterface

Stream representation of the uploaded file.

getTemporaryFileName()

Retrieve the temporary file name (for example /tmp/tmp_foo_filexyz If the file has been moved (by moveTo) an exception is thrown.

public getTemporaryFileName() : string|null
Internal

Not part of the PSR interface - used for legacy code in the core

Return values
string|null

moveTo()

Move the uploaded file to a new location.

public moveTo(string $targetPath) : void

Use this method as an alternative to move_uploaded_file(). This method is guaranteed to work in both SAPI and non-SAPI environments. Implementations must determine which environment they are in, and use the appropriate method (move_uploaded_file(), rename(), or a stream operation) to perform the operation.

$targetPath may be an absolute path, or a relative path. If it is a relative path, resolution should be the same as used by PHP's rename() function.

The original file or stream MUST be removed on completion.

If this method is called more than once, any subsequent calls MUST raise an exception.

When used in an SAPI environment where $_FILES is populated, when writing files via moveTo(), is_uploaded_file() and move_uploaded_file() SHOULD be used to ensure permissions and upload status are verified correctly.

If you wish to move to a stream, use getStream(), as SAPI operations cannot guarantee writing to stream destinations.

Parameters
$targetPath : string

Path to which to move the uploaded file.

Tags
see
https://php.net/is_uploaded_file
see
https://php.net/move_uploaded_file
throws
InvalidArgumentException

if the $path specified is invalid.

throws
RuntimeException

on any error during the move operation, or on the second or subsequent call to the method.


        
On this page

Search results