\Hazaar\File
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’, array( ‘created’ => ‘Now()’, ‘filename’ => $upload->my_new_file[‘name’], ‘bytes’ => $bytes ));

}

Summary
Methods Properties Constants
get
has
No constants
Properties
$files
$files
Methods
__construct()
__construct()
Constructor
__get()
__get($key) : Array
Magic method to get details about an uploaded file

Parameters

$keystring

The key of the uploaded file to return details of.

Returns

Array
get()
get($key) : Array
Get details about an uploaded file.

Parameters

$keystring

The key of the uploaded file to return details of.

Returns

Array
getFile()
getFile($key) : \Hazaar\File
Returns the uploaded file as a Hazaar\File object.

Parameters

$keymixed

No description

Returns

\Hazaar\File
has()
has($key) : boolean
Test the existence of a file upload key

Parameters

$keystring

The key to check for

Returns

boolean
keys()
keys() : Array
Return an array of uploaded file keys

The keys are the ‘name’ attribute for the form element that was used to upload the file.

Returns

Array
read()
read($key) : string
Read the contents of an uploaded file and return the bytes

Parameters

$keystring

The key name of the file to return. Use \Hazaar\Upload\File::keys() to get this.

Returns

string

The bytes for the uploaded file.

save()
save($key, $destination, $overwrite = false) : boolean
Save an uploaded file to a destination

Parameters

$keystring

The index key of the file to save

$destinationstring

The destination file or directory to save the file to

$overwriteboolean

Flag to indicate if existing files should be overwritten

Returns

boolean
saveAll()
saveAll($destination, $overwrite = false, $callback = null) : void
Save all the files that were uploaded to a single directory

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

$destinationstring

The destination directory into which we copy the files

$overwrite

No description

$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.

Returns

\Hazaar\File\void
uploaded()
uploaded($op_keys = null) : boolean
Check to see if there are any files that have uploaded as part of the current request

Parameters

$op_keysMixed

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.

Returns

boolean