Class
UploadedFile
A PSR-7 file uploaded with a request.
Introduction#
Made from a stream resource, a file path (as $_FILES['tmp_name'] gives it) or a StreamInterface.
The client's file name and media type are what the client sent: do not trust them.
Class synopsis#
class UploadedFile implements UploadedFileInterface {
/* Methods */
public UploadedFile::__construct(
mixed $source,
?string $clientFilename = null,
?string $clientMediaType = null,
?int $size = null,
?int $error = null,
bool $isUploadedFile = false
)
public UploadedFile::getStream(): StreamInterface
public UploadedFile::moveTo($targetPath): void
public UploadedFile::getSize(): ?int
public UploadedFile::getError(): int
public UploadedFile::getClientFilename(): ?string
public UploadedFile::getClientMediaType(): ?string
}Methods#
| Method | Description |
|---|---|
__construct | Creates an uploaded file. |
getStream | Retrieve a stream representing the uploaded file. |
moveTo | Moves the file to $targetPath, once. |
getSize | Retrieve the file size. |
getError | Retrieve the error associated with the uploaded file. |
getClientFilename | Retrieve the filename sent by the client. |
getClientMediaType | Retrieve the media type sent by the client. |
Examples#
$file = new UploadedFile($_FILES['avatar']['tmp_name'], $_FILES['avatar']['name'], $_FILES['avatar']['type'], $_FILES['avatar']['size'], $_FILES['avatar']['error']);
if (UPLOAD_ERR_OK === $file->getError()) {
$file->moveTo('/var/uploads/avatar.png');
}See also#
ServerRequest::getUploadedFilesRetrieve normalized file upload data.