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
MethodsPropertiesConstants
No constants
Properties
$files
$files
Methods
__construct()
__construct()
Constructor
__get()
__get($key) : Array
Magic method to get details about an uploaded file
get()
get($key = null) : Array
Get details about an uploaded file.
getFile()
getFile($key = null) : \Hazaar\File|array
Returns the uploaded file as a Hazaar\File object.
Static
getMaxUploadSize()
getMaxUploadSize()
has()
has($key) : boolean
Test the existence of a file upload key
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.

read()
read($key) : string
Read the contents of an uploaded file and return the bytes
resolveFiles()
resolveFiles($array)
save()
save($key, $destination, $overwrite = false) : boolean
Save an uploaded file to a destination
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;

});

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