Simple File uploading library capable of handling multiple file uploads
Installing Add file to Composer.json file:
{
"require": {
"eeshiro/file-upload": "~1.0.0"
}
}
or
composer require eeshiro/file-upload
require './vendor/autoload.php';
$upload = new FileUpload;
$single_file = $upload->validate('single_file', 'Single File')
->required()
->single()
->max_size(2)
->get();
$multiple_file= $upload->validate('multiple_file', 'Multiple File')
->required()
->multiple()
->max_size(2, 'MB')
->max_file(3)
->get();
if($upload->has_error()){
echo $upload->get_errors('<br>');
die();
}
$upload->move_uploaded_file($single_file, 'directory/filename.txt');
foreach ($multiple_file as $key => $file) {
$upload->move_uploaded_file($file, 'directory/'.$file['name']);
}
Rule | Parameter | Description | Example |
---|---|---|---|
require | No | Returns FALSE if the form element is empty. | |
single | No | Returns FALSE if file upload type multiple. | |
multiple | No | Returns FALSE if file upload type single. | |
max_file | Yes | Returns FALSE if file count is greater than maximum number of files specified. | max_file(5) |
min_file | Yes | Returns FALSE if file count is less than minimum number of files specified. | min_file(2) |
image | No | Returns FALSE if uploaded file is not an image. | |
exist | Yes | Returns FALSE if file already exist on the directory. | exist(directory) |
min_size | Yes | Returns FALSE file size is under the minimum size specified. Parameter 1 : size (Integer). Parameter 2 : size Type (String) Available size type (KB, MB, GB, TB) | min_size(2, 'MB') |
max_size | Yes | Returns FALSE if file exceeds the maximum size specified. Parameter 1 : size (Integer). Parameter 2 : size Type (String) Available size type (KB, MB, GB, TB) | min_size(2, 'MB') |
valid_format | Yes | Returns FALSE if file format is not in the list specified. Parameter 1 : file type list (Array) | valid_format(['jpg', 'png', 'gif']) |
move | Yes | Moves the validated file to the directory specified. Returns TRUE if successfully saved, FALSE if not. Parameter 1 : directory (String). Parameter 2 : use original filename (Boolean) (true if retain, false if not). | move('directory/filename.txt', false) |
get | No | Returns the file validated. | |
has_error | Yes | Returns TRUE if validation has error. Parameter 1: file index / file field name (String). NOT REQUIRED. | |
error_count | Yes | Returns NUMBER of errors on validation. Parameter 1: file index / file field name (String). NOT REQUIRED. | |
get_errors | Yes | Return validation errors. Parameter 1: glue (String) (If NULL returns array of errors). Parameter 1: file index / file field name (String) (NOT required). | get_errors(' ') |
move_uploaded_file | Yes | Moves the file to the directory specified. Parameter 1 : File from get method (Array). Parameter 2 : File directory. | move_uploaded_file($file, 'directory/'.$file['name']) |