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);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
Execute the __construct operation for this file component.
public __construct(): void__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. If a callback function is specified then that function will be executed for each file. Arguments to the function are the key, $name, $size and $type. If the callback function returns false, the file is NOT copied. The $name argument is also checked after the function call to give the callback function a chance to alter the destination filename (see example).
$files->saveAll('/home/user', false, function($key, &$name, $size, $type)){
if($type == 'image/jpeg'){
$name = uniqid() . '.jpeg';
return true;
}
return false;
});Parameters
| Parameter | Type | Description |
|---|---|---|
$destination | string | The destination directory into which we copy the files |
$overwrite | bool | |
$callback | Closure | A callback function that will be called for each file. This function must return true for the file to be copied. The $name field is passed byRef so the file can be renamed on the way through. |
save
Save an uploaded file to a destination.
public save(string $key, string $destination, bool $overwrite): voidParameters
| 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
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string | The key name of the file to return. Use \Hazaar\Upload\File::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
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string |
getMaxUploadSize
Returns the effective maximum upload size in bytes.
public getMaxUploadSize(): intReturns: int
The value is computed from the stricter of post_max_size and upload_max_filesize when both are set.
resolveFiles
private resolveFiles(array $array): array|FileReturns: array|Hazaar\File
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array |
Generated by Hazaar API Doc Generator on Fri, 04 Sep 2026 23:32:17 +0000