The upload class simplifies the process of uploading files to the server. You can instantiate a new Uploader the following way:
$uploader = new \Utilities\Uploader();
Then you can user the following methods.
Just remember to
You can set the maximum allowed file size (expressed in Kb):
You can get the maximum allowed file size this way:
By default every file is uploaded to the server root folder. But you can change the default directory this way:
This will upload any file to the public/images folder.
You can get the choosen directory this way:
To get information about the last file uploaded, you can use the
That will return an array with information about the file name, file type, file size, directory, uploader IP, upload date and any upload error.
You can validate a file format using the
It will return true if the file matches the formats especified:
$matches = $uploader->matchFormat('examplefile', 'jpg, png, gif'));
You can pass a string separated by commas or an array like this:
$formats = array('jpg', 'png', 'gif'); $matches = $uploader->matchFormat('examplefile', $formats));
First create a form in your view like this:
<form action="yourpage/upload" method="post" enctype="multipart/form-data"> <input type="file" name="examplefile"/> <input type="submit" value="upload"/> </form>
Then in your controller create a function named
upload with the following code:
//Optional configuration $uploader->setMaxSize(1000)->setDirectory(''); //File upload $uploader->file('examplefile');
If the examplefile size isn't greater than 1000 Kb it will be uploaded to the public directory. You are done!