Skip to content

Commit 7ed298f

Browse files
authored
Merge pull request #5508 from kenjis/fix-uploaded-file
docs: improve File Uploading
2 parents efc1bca + d654114 commit 7ed298f

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

user_guide_src/source/incoming/incomingrequest.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ and uses best practices to minimize any security risks.
293293
$files = $request->getFiles();
294294

295295
// Grab the file by name given in HTML form
296-
if ($files->hasFile('uploadedFile')) {
297-
$file = $files->getFile('uploadedfile');
296+
if ($files->hasFile('userfile')) {
297+
$file = $files->getFile('userfile');
298298

299299
// Generate a new secure name
300300
$name = $file->getRandomName();
@@ -309,12 +309,14 @@ and uses best practices to minimize any security risks.
309309

310310
You can retrieve a single file uploaded on its own, based on the filename given in the HTML file input::
311311

312-
$file = $request->getFile('uploadedfile');
312+
$file = $request->getFile('userfile');
313313

314314
You can retrieve an array of same-named files uploaded as part of a
315315
multi-file upload, based on the filename given in the HTML file input::
316316

317-
$files = $request->getFileMultiple('uploadedfile');
317+
$files = $request->getFileMultiple('userfile');
318+
319+
.. note:: The files here correspond to ``$_FILES``. Even if a user just clicks submit button of a form and does not upload any file, the file will still exist. You can check that the file was actually uploaded by the ``isValid()`` method in UploadedFile. See :ref:`verify-a-file` for more details.
318320

319321
Content Negotiation
320322
-------------------

user_guide_src/source/libraries/uploaded_files.rst

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ Which would return a simple array like::
194194
'avatar' => // UploadedFile instance
195195
]
196196

197+
.. note:: The UploadedFile instance corresponds to ``$_FILES``. Even if a user just clicks the submit button and does not upload any file, the instance will still exist. You can check that the file was actually uploaded by the ``isValid()`` method in UploadedFile. See :ref:`verify-a-file`.
198+
197199
If you used an array notation for the name, the input would look something like::
198200

199201
<input type="file" name="my-form[details][avatar]" />
@@ -231,7 +233,7 @@ Single File
231233
If you just need to access a single file, you can use ``getFile()`` to retrieve the file instance directly. This will return an instance of ``CodeIgniter\HTTP\Files\UploadedFile``:
232234

233235
Simplest usage
234-
^^^^^^^^^^^^^^
236+
--------------
235237

236238
With the simplest usage, a single file might be submitted like::
237239

@@ -242,7 +244,7 @@ Which would return a simple file instance like::
242244
$file = $this->request->getFile('userfile');
243245

244246
Array notation
245-
^^^^^^^^^^^^^^
247+
--------------
246248

247249
If you used an array notation for the name, the input would look something like::
248250

@@ -253,7 +255,8 @@ For get the file instance::
253255
$file = $this->request->getFile('my-form.details.avatar');
254256

255257
Multiple files
256-
^^^^^^^^^^^^^^
258+
==============
259+
257260
::
258261

259262
<input type="file" name="images[]" multiple />
@@ -269,9 +272,10 @@ In controller::
269272
}
270273
}
271274

272-
where the **images** is a loop from the form field name
275+
where the ``images`` is a loop from the form field name.
276+
277+
If there are multiple files with the same name you can use ``getFile()`` to retrieve every file individually.
273278

274-
If there are multiple files with the same name you can use ``getFile()`` to retrieve every file individually::
275279
In controller::
276280

277281
$file1 = $this->request->getFile('images.0');
@@ -301,7 +305,9 @@ Working With the File
301305
Once you've retrieved the UploadedFile instance, you can retrieve information about the file in safe ways, as well as
302306
move the file to a new location.
303307

304-
Verify A File
308+
.. _verify-a-file:
309+
310+
Verify a File
305311
=============
306312

307313
You can check that a file was actually uploaded via HTTP with no errors by calling the ``isValid()`` method::

0 commit comments

Comments
 (0)