The upload class simplifies the process of uploading files to the server. Every Controller class has its own upload class.
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 public 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 = $this->upload->matchFormat('examplefile', 'jpg, png, gif'));
You can pass a string separated by commas or an array like this:
$formats = array('jpg', 'png', 'gif'); $matches = $this->upload->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 $this->upload->setMaxSize(1000)->setDirectory(''); $this->upload->file('examplefile');
If the examplefile size isn't greater than 1000 Kb it will be uploaded to the public folder. You are done!