22Working with Uploaded Files
33###########################
44
5- CodeIgniter makes working with files uploaded through a form much simpler and more secure than using PHP's ``$_FILES ``
6- array directly. This extends the :doc: `File class </libraries/files >` and thus gains all of the features of that class.
5+ CodeIgniter makes working with files uploaded through a form much simpler and
6+ more secure than using PHP's ``$_FILES `` array directly. This extends the
7+ :doc: `File class </libraries/files >` and thus gains all of the features of that
8+ class.
79
810.. note :: This is not the same as the File Uploading class in CodeIgniter 3.
911 This provides a raw interface to the uploaded files with a few small features.
@@ -12,9 +14,11 @@ array directly. This extends the :doc:`File class </libraries/files>` and thus g
1214 :local:
1315 :depth: 2
1416
15- ***********
16- The Process
17- ***********
17+ .. _file-upload-form-tutorial :
18+
19+ *************************
20+ File Upload Form Tutorial
21+ *************************
1822
1923Uploading a file involves the following general process:
2024
@@ -33,21 +37,23 @@ Creating the Upload Form
3337========================
3438
3539Using a text editor, create a form called **upload_form.php **. In it, place
36- this code and save it to your **app/Views/ ** directory:
40+ this code and save it to your **app/Views ** directory:
3741
3842.. literalinclude :: uploaded_files/001.php
3943
4044You'll notice we are using a form helper to create the opening form tag.
4145File uploads require a multipart form, so the helper creates the proper
42- syntax for you. You'll also notice we have an ``$errors `` variable. This is
46+ syntax for you.
47+
48+ You'll also notice we have an ``$errors `` variable. This is
4349so we can show error messages in the event the user does something
4450wrong.
4551
4652The Success Page
4753================
4854
4955Using a text editor, create a form called **upload_success.php **. In it,
50- place this code and save it to your **app/Views/ ** directory::
56+ place this code and save it to your **app/Views ** directory::
5157
5258 <!DOCTYPE html>
5359 <html lang="en">
@@ -73,13 +79,23 @@ The Controller
7379==============
7480
7581Using a text editor, create a controller called **Upload.php **. In it, place
76- this code and save it to your **app/Controllers/ ** directory:
82+ this code and save it to your **app/Controllers ** directory:
7783
7884.. literalinclude :: uploaded_files/002.php
7985
80- .. note :: Since the value of a file upload HTML field doesn't exist, and is stored in the ``$_FILES`` global,
81- only :ref: `rules-for-file-uploads ` can be used to validate upload file with :doc: `validation `.
82- The rule ``required `` also can't be used, so use ``uploaded `` instead.
86+ Since the value of a file upload HTML field doesn't exist, and is stored in the
87+ ``$_FILES `` global, only :ref: `rules-for-file-uploads ` can be used to validate
88+ upload file with :doc: `validation `.
89+
90+ The rule ``required `` cannot be used either, so if the file is required, use
91+ the rule ``uploaded `` instead.
92+
93+ Note that an empty array (``[] ``) is passed as the first argument to
94+ ``$this->validateData() ``. It is because the file validation rules get the data
95+ for the uploaded file directly from the Request object.
96+
97+ If the form has fields other than file upload, pass the field data as the first
98+ argument.
8399
84100The Routes
85101==========
0 commit comments