-
Notifications
You must be signed in to change notification settings - Fork 2
UploadedFile
Viames Marino edited this page Apr 28, 2026
·
2 revisions
Pair\Http\UploadedFile represents one HTTP uploaded file.
It replaces the legacy Pair\Helpers\Upload helper in Pair v4 and keeps upload handling in the HTTP layer.
Builds an object from $_FILES[$fieldName].
Builds an object from one normalized $_FILES entry.
fromLocalFile(string $path, ?string $clientFilename = null, ?string $clientMediaType = null, ?string $fieldName = null): UploadedFile
Builds an object around a trusted local file for tests, CLI flows, or internal imports.
fieldName(): ?stringclientFilename(): stringsafeClientFilename(): stringtemporaryPath(): stringsize(): interror(): intisOk(): boolerrorMessage(): ?stringclientMediaType(): ?stringdetectedMediaType(): ?stringmediaType(): ?stringmediaCategory(): ?stringextension(): ?stringchecksum(string $algorithm = 'md5'): string
Moves the upload to a local directory and returns the final path.
Behavior:
- validates the PHP upload error before moving
- uses
move_uploaded_file()for real HTTP uploads - sanitizes the destination filename
- creates missing destination folders with
0775 - stores files with
0664 - avoids overwriting by appending a numeric suffix unless
$overwriteistrue
Uploads the temporary file directly to S3 through Pair\Services\AmazonS3.
Local storage:
use Pair\Http\UploadedFile;
$upload = UploadedFile::fromGlobals('avatar');
$path = $upload->moveTo(APPLICATION_PATH . '/public/uploads');Custom filename:
$path = $upload->moveTo(
APPLICATION_PATH . '/public/uploads',
'user-42-avatar.png'
);S3 storage:
$upload->putToS3('avatars/user-42.png', $amazonS3);Validation example:
if ('image' !== $upload->mediaCategory()) {
throw new \RuntimeException('Avatar must be an image.');
}Upload error handling:
$upload = UploadedFile::fromGlobals('avatar');
if (!$upload->isOk()) {
throw new \RuntimeException($upload->errorMessage() ?? 'Upload failed.');
}
$checksum = $upload->checksum('sha256');See also: FileMediaType, AmazonS3, File, Form.