Upload
Upload
The file upload manager class.
class UploadThis class makes it much simpler to work with file uploaded via a POST request to the server. Behind the scenes it works with the PHP $_FILES global that contains information about any uploaded files. This class then provides a few simple methods to make saving all files, or a single file, much simpler.
Examples
Save all uploaded files to a directory:
$upload = new \Hazaar\File\Upload();
$upload->saveAll('/home/user', true);From a controller, the current request provides an instance via Hazaar\Application\Request::files:
$this->request->files()->save('avatar', '/home/user/avatars');Save a single file into a database if it has been uploaded:
$upload = new \Hazaar\File\Upload();
if($upload->has('my_new_file')){
$bytes = $upload->read('my_new_file');
$db->insert('file_contents', [
'created' => 'Now()',
'filename' => $upload->my_new_file['name'],
'bytes' => $bytes
]);
}Properties
files
The uploaded files array.
private array $filesType: array
Methods
__construct
Create a new upload manager.
public __construct(?array $files): voidParameters
| Parameter | Type | Description |
|---|---|---|
$files | array | The uploaded files array in $_FILES format. Defaults to $_FILES. |
__get
Magic method to get details about an uploaded file.
public __get(string $key): arrayReturns: array
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key of the uploaded file to return details of |
uploaded
Check to see if there are any files that have uploaded as part of the current request.
public uploaded(array|string|null $opKeys): voidParameters
| Parameter | Type | Description |
|---|---|---|
$opKeys | array | string |
keys
Return an array of uploaded file keys.
public keys(): arrayReturns: array
The keys are the 'name' attribute for the form element that was used to upload the file.
has
Test the existence of a file upload key.
public has(string $key): voidParameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The key to check for |
get
Get details about an uploaded file.
public get(?string $key): ?arrayReturns: array
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the key of the uploaded file to return details of |
saveAll
Save all the files that were uploaded to a single directory.
public saveAll(string $destination, bool $overwrite, ?\Closure $callback): voidReturns: void
This will iterate through all uploaded files and save them to the specified destination directory using the original client filename. File inputs that were left empty are skipped. If a callback function is specified then it will be executed for each file with the form element key and a \Hazaar\File object for the upload. The callback must return true for the file to be saved.
$files->saveAll('/home/user', false, function(string $key, \Hazaar\File $file): bool {
return 'image/jpeg' === $file->mimeContentType();
});Parameters
| Parameter | Type | Description |
|---|---|---|
$destination | string | The destination directory into which we copy the files |
$overwrite | bool | Flag to indicate if existing files should be overwritten |
$callback | Closure | A callback function that will be called for each file. This function must return true for the file to be saved. |
save
Save an uploaded file to a destination.
public save(string $key, string $destination, bool $overwrite): boolReturns: bool
If the destination is an existing directory the file is saved into it using the original client filename, otherwise the destination is used as the full target path. Keys for multi-file inputs (name="docs[]") save every file in the input, so the destination should be a directory. Dot-notation keys (docs.0) are supported.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The index key of the file to save |
$destination | string | The destination file or directory to save the file to |
$overwrite | bool | Flag to indicate if existing files should be overwritten |
read
Read the contents of an uploaded file and return the bytes.
public read(string $key): ?stringReturns: string
For multi-file inputs (name="docs[]") use dot-notation to select a single file, eg: docs.0.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The key name of the file to return. Use \Hazaar\File\Upload::keys() to get this. |
getFile
Returns the uploaded file as a Hazaar\File object.
public getFile(?string $key): array|false|FileReturns: array|false|Hazaar\File
Multi-file inputs return an array of File objects. File inputs that were left empty are omitted.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string |
getMaxUploadSize
Returns the effective maximum upload size in bytes.
public getMaxUploadSize(): intReturns: int
The value is the smaller of post_max_size and upload_max_filesize, ignoring either one that is unlimited (zero). Returns -1 if neither imposes a limit.
resolveFiles
private resolveFiles(array $array): array|FileReturns: array|Hazaar\File
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array |
leaves
Flattens normalised upload info into a list of the individual files that were actually uploaded.
private leaves(array $array): arrayReturns: array
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array |
store
Copies a single uploaded file to its destination.
private store(array $info, string $destination, bool $overwrite): stringReturns: string
Parameters
| Parameter | Type | Description |
|---|---|---|
$info | array | |
$destination | string | |
$overwrite | bool |
Generated by Hazaar API Doc Generator on Thu, 22 Oct 2026 05:05:29 +0000