CodeIgniter makes working with files uploaded through a form much simpler and more secure than using PHP’s $_FILES
array directly. This extends the File class and thus gains all of the features of that class.
Note
This is not the same as the File Uploading class in CodeIgniter v3.x. This provides a raw interface to the uploaded files with a few small features.
The Process
Uploading a file involves the following general process:
An upload form is displayed, allowing a user to select a file and upload it.
When the form is submitted, the file is uploaded to the destination you specify.
Along the way, the file is validated to make sure it is allowed to be uploaded based on the preferences you set.
Once uploaded, the user will be shown a success message.
To demonstrate this process here is brief tutorial. Afterward you’ll find reference information.
Creating the Upload Form
Using a text editor, create a form called upload_form.php. In it, place this code and save it to your app/Views/ directory:
DOCTYPE html>
Upload Form
You’ll notice we are using a form helper to create the opening form tag. File uploads require a multipart form, so the helper creates the proper syntax for you. You’ll also notice we have an $errors
variable. This is so we can show error messages in the event the user does something wrong.
The Success Page
Using a text editor, create a form called upload_success.php. In it, place this code and save it to your app/Views/ directory:
DOCTYPE html>
Upload Form
Your file was successfully uploaded!
name:
size: KB
extension:
The Controller
Using a text editor, create a controller called Upload.php. In it, place this code and save it to your app/Controllers/ directory: