Class UploadedFile
PSR-7 uploaded file implementation.
Constants
-
array<int, string>ERROR_MESSAGES ¶[UPLOAD_ERR_OK => 'There is no error, the file uploaded with success', UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini', UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was ' . 'specified in the HTML form', UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded', UPLOAD_ERR_NO_FILE => 'No file was uploaded', UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder', UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk', UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the file upload']Human readable messages for the
UPLOAD_ERR_*constants.
Property Summary
Method Summary
-
__construct() public
Constructor
-
assertMovable() protected
Ensure the upload succeeded and hasn't been moved yet.
-
copyStream() protected
Copy a readable upload into an open destination, checking every write.
-
getClientFilename() public
Retrieve the filename sent by the client.
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.
Implementations SHOULD return the value stored in the "name" key of the file in the $_FILES array.
-
getClientMediaType() public
Retrieve the media type sent by the client.
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.
Implementations SHOULD return the value stored in the "type" key of the file in the $_FILES array.
-
getError() public
Retrieve the error associated with the uploaded file.
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.
Implementations SHOULD return the value stored in the "error" key of the file in the $_FILES array.
-
getSize() public
Retrieve the file size.
Implementations SHOULD return 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.
-
getStream() public
Retrieve a stream representing the uploaded file.
This method MUST return 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 MUST raise an exception.
-
moveTo() public
Move the uploaded file to a new location.
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.
-
writeFile() protected
Write the uploaded file stream to the given path.
Method Detail
__construct() ¶ public
__construct(Psr\Http\Message\StreamInterface|resource|string $streamOrFile, int|null $size, int $error, string|null $clientFilename = null, string|null $clientMediaType = null)
Constructor
Parameters
-
Psr\Http\Message\StreamInterface|resource|string$streamOrFile The file path, resource or stream.
-
int|null$size The file size in bytes.
-
int$error One of the
UPLOAD_ERR_*constants.-
string|null$clientFilename optional The filename as provided by the client.
-
string|null$clientMediaType optional The media type as provided by the client.
Throws
InvalidArgumentExceptionWhen the stream/file or error status is invalid.
assertMovable() ¶ protected
assertMovable(): void
Ensure the upload succeeded and hasn't been moved yet.
Returns
voidThrows
RuntimeExceptioncopyStream() ¶ protected
copyStream(Psr\Http\Message\StreamInterface $stream, resource $handle, string $path): void
Copy a readable upload into an open destination, checking every write.
Parameters
-
Psr\Http\Message\StreamInterface$stream The upload source.
-
resource$handle The destination handle.
-
string$path The destination path for error messages.
Returns
voidgetClientFilename() ¶ public
getClientFilename(): string|null
Retrieve the filename sent by the client.
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.
Implementations SHOULD return the value stored in the "name" key of the file in the $_FILES array.
Returns
string|nullgetClientMediaType() ¶ public
getClientMediaType(): string|null
Retrieve the media type sent by the client.
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.
Implementations SHOULD return the value stored in the "type" key of the file in the $_FILES array.
Returns
string|nullgetError() ¶ public
getError(): int
Retrieve the error associated with the uploaded file.
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.
Implementations SHOULD return the value stored in the "error" key of the file in the $_FILES array.
Returns
intgetSize() ¶ public
getSize(): int|null
Retrieve the file size.
Implementations SHOULD return 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.
Returns
int|nullgetStream() ¶ public
getStream(): StreamInterface
Retrieve a stream representing the uploaded file.
This method MUST return 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 MUST raise an exception.
Returns
StreamInterfacemoveTo() ¶ public
moveTo(string $targetPath): void
Move the uploaded file to a new location.
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
-
string$targetPath
Returns
voidwriteFile() ¶ protected
writeFile(string $path): void
Write the uploaded file stream to the given path.
Parameters
-
string$path The target path.
Returns
voidThrows
RuntimeExceptionWhen the target path can't be opened.