phasync
Streams and files / UploadedFile

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#

MethodDescription
__constructCreates an uploaded file.
getStreamRetrieve a stream representing the uploaded file.
moveToMoves the file to $targetPath, once.
getSizeRetrieve the file size.
getErrorRetrieve the error associated with the uploaded file.
getClientFilenameRetrieve the filename sent by the client.
getClientMediaTypeRetrieve the media type sent by the client.

Examples#

Example 1
$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#