UploadTrait
Provides helpers to receive PSR-7 file uploads, validate them and store them on disk.
Both helpers delegate the per-file work (error/size/MIME checks, name sanitization, move) to the shared self::storeUploadedFile() method and throw a FileException on any failure, leaving the response shaping to the controller.
Table of Contents
Methods
- receiveUpload() : string
- Receives a single uploaded file for the given field, validates it and stores it.
- receiveUploads() : array<string|int, string>
- Receives multiple uploaded files for the given field, validates them and stores them.
- storeUploadedFile() : string
- Validates a single uploaded file and moves it into the destination directory.
- uploadErrorMessage() : string
- Maps a PHP `UPLOAD_ERR_*` code to a human-readable message.
Methods
receiveUpload()
Receives a single uploaded file for the given field, validates it and stores it.
public
receiveUpload(ServerRequestInterface $request, string $field, string $destDir[, array<string|int, mixed> $options = [] ]) : string
Parameters
- $request : ServerRequestInterface
-
The PSR-7 request carrying the uploaded files.
- $field : string
-
The uploaded-file field name.
- $destDir : string
-
The destination directory.
- $options : array<string|int, mixed> = []
-
Optional switches (see UploadOption).
Tags
Return values
string —The absolute path of the stored file.
receiveUploads()
Receives multiple uploaded files for the given field, validates them and stores them.
public
receiveUploads(ServerRequestInterface $request, string $field, string $destDir[, array<string|int, mixed> $options = [] ]) : array<string|int, string>
The UploadOption::FILENAME option is ignored here: each file keeps its own sanitized client name to avoid collisions.
Parameters
- $request : ServerRequestInterface
-
The PSR-7 request carrying the uploaded files.
- $field : string
-
The uploaded-file field name (an array of files).
- $destDir : string
-
The destination directory.
- $options : array<string|int, mixed> = []
-
Optional switches (see UploadOption).
Tags
Return values
array<string|int, string> —The absolute paths of the stored files.
storeUploadedFile()
Validates a single uploaded file and moves it into the destination directory.
private
storeUploadedFile(UploadedFileInterface $uploaded, string $destDir[, array<string|int, mixed> $options = [] ]) : string
Parameters
- $uploaded : UploadedFileInterface
-
The uploaded file.
- $destDir : string
-
The destination directory.
- $options : array<string|int, mixed> = []
-
Optional switches (see UploadOption).
Tags
Return values
string —The absolute path of the stored file.
uploadErrorMessage()
Maps a PHP `UPLOAD_ERR_*` code to a human-readable message.
private
uploadErrorMessage(int $error) : string
Parameters
- $error : int
-
One of the
UPLOAD_ERR_*constants.
Return values
string —The associated message.