You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$data = ['uploaded_flleinfo' => new File($filepath)];
139
+
140
+
return view('upload_success', $data);
141
+
} else {
142
+
$data = ['errors' => 'The file has already been moved.'];
143
+
144
+
return view('upload_form', $data);
145
+
}
146
+
}
147
+
}
148
+
149
+
.. note:: Since the value of a file upload HTML field doesn't exist, and is stored in the ``$_FILES`` global,
150
+
only :ref:`rules-for-file-uploads` can be used to validate upload file with :doc:`validation`.
151
+
The rule ``required`` also can't be used, so use ``uploaded`` instead.
152
+
153
+
The Upload Directory
154
+
====================
155
+
156
+
The uploaded files are stored in the **writable/uploads/** directory.
157
+
158
+
Try it!
159
+
=======
160
+
161
+
To try your form, visit your site using a URL similar to this one::
162
+
163
+
example.com/index.php/upload/
164
+
165
+
You should see an upload form. Try uploading an image file (either a
166
+
**jpg**, **gif**, **png**, or **webp**). If the path in your controller is correct it should
167
+
work.
168
+
169
+
***************
16
170
Accessing Files
17
-
===============
171
+
***************
18
172
19
173
All Files
20
-
----------
174
+
=========
21
175
22
176
When you upload files they can be accessed natively in PHP through the ``$_FILES`` superglobal. This array has some
23
177
major shortcomings when working with multiple files uploaded at once, and has potential security flaws many developers
@@ -72,7 +226,7 @@ In this case, the returned array of files would be more like::
72
226
]
73
227
74
228
Single File
75
-
-----------
229
+
===========
76
230
77
231
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``:
78
232
@@ -110,14 +264,14 @@ In controller::
110
264
foreach($imagefile['images'] as $img) {
111
265
if ($img->isValid() && ! $img->hasMoved()) {
112
266
$newName = $img->getRandomName();
113
-
$img->move(WRITEPATH.'uploads', $newName);
267
+
$img->move(WRITEPATH . 'uploads', $newName);
114
268
}
115
269
}
116
270
}
117
271
118
272
where the **images** is a loop from the form field name
119
273
120
-
If there are multiple files with the same name you can use ``getFile()`` ro retrieve every file individually::
274
+
If there are multiple files with the same name you can use ``getFile()`` to retrieve every file individually::
0 commit comments