Skip to content

Commit 2977b02

Browse files
authored
Merge pull request #8876 from kenjis/docs-improve-file-upload-validation
docs: improve file upload validation
2 parents eafbcd8 + 312a951 commit 2977b02

2 files changed

Lines changed: 36 additions & 15 deletions

File tree

user_guide_src/source/libraries/uploaded_files.rst

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
Working 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

1923
Uploading a file involves the following general process:
2024

@@ -33,21 +37,23 @@ Creating the Upload Form
3337
========================
3438

3539
Using 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

4044
You'll notice we are using a form helper to create the opening form tag.
4145
File 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
4349
so we can show error messages in the event the user does something
4450
wrong.
4551

4652
The Success Page
4753
================
4854

4955
Using 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

7581
Using 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

84100
The Routes
85101
==========

user_guide_src/source/libraries/validation.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,8 @@ Available Rules
885885
.. literalinclude:: validation/038.php
886886
:lines: 2-
887887

888+
.. _rules-for-general-use:
889+
888890
Rules for General Use
889891
=====================
890892

@@ -1044,9 +1046,10 @@ file validation.
10441046
files. Therefore, adding any general rules, like ``permit_empty``, to file
10451047
validation rules array or string, the file validation will not work correctly.
10461048

1047-
Since the value of a file upload HTML field doesn't exist, and is stored in the ``$_FILES`` global, the name of the input field will
1048-
need to be used twice. Once to specify the field name as you would for any other rule, but again as the first parameter of all
1049-
file upload related rules::
1049+
Since the value of a file upload HTML field doesn't exist, and is stored in the
1050+
``$_FILES`` global, the name of the input field will need to be used twice. Once
1051+
to specify the field name as you would for any other rule, but again as the first
1052+
parameter of all file upload related rules::
10501053

10511054
// In the HTML
10521055
<input type="file" name="avatar">
@@ -1056,6 +1059,8 @@ file upload related rules::
10561059
'avatar' => 'uploaded[avatar]|max_size[avatar,1024]',
10571060
]);
10581061

1062+
See also :ref:`file-upload-form-tutorial`.
1063+
10591064
======================= ========== ============================================= ===================================================
10601065
Rule Parameter Description Example
10611066
======================= ========== ============================================= ===================================================

0 commit comments

Comments
 (0)