Upload
Upload
The file upload manager class.
This 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 $files
Methods
__construct
Constructor.
public __construct({Array})
__get
Magic method to get details about an uploaded file.
public __get({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})
Parameters
Parameter | Type | Description |
---|---|---|
$op_keys | NULL | A key, or array of keys to check for. If any of the supplied keys do not exist then the |
method will return false. If this parameter is not supplied this method will return true
if ANY file has been uploaded. |
keys
Return an array of uploaded file keys.
public keys({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({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$key | string | The key to check for |
get
Get details about an uploaded file.
public get({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({Array})
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({Array})
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 | boolean | Flag to indicate if existing files should be overwritten |
read
Read the contents of an uploaded file and return the bytes.
public read({Array})
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({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$key | string | |
$ | File |
getMaxUploadSize
public getMaxUploadSize({Array})
resolveFiles
private resolveFiles({Array})
Parameters
Parameter | Type | Description |
---|---|---|
$array | array | |
$ | File |
Generated by Hazaar API Doc Generator