blob: 810338d3876fcfd399daf53f69ad8babe19281b5 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html>
3<head>
4
Derek Allard404e35d2007-08-07 01:00:45 +00005<title>CodeIgniter User Guide : File Uploading Class</title>
adminb0dd10f2006-08-25 17:25:49 +00006
7<style type='text/css' media='all'>@import url('../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
9
admin17a890d2006-09-27 20:42:42 +000010<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000011<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000012<script type="text/javascript" src="../nav/moo.fx.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000013<script type="text/javascript">
14window.onload = function() {
admine334c472006-10-21 19:44:22 +000015 myHeight = new fx.Height('nav', {duration: 400});
adminb0dd10f2006-08-25 17:25:49 +000016 myHeight.hide();
17}
18</script>
19
20<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
21<meta http-equiv='expires' content='-1' />
22<meta http-equiv= 'pragma' content='no-cache' />
23<meta name='robots' content='all' />
24<meta name='author' content='Rick Ellis' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000025<meta name='description' content='CodeIgniter User Guide' />
adminb0dd10f2006-08-25 17:25:49 +000026
27</head>
28<body>
29
30<!-- START NAVIGATION -->
31<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
32<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
33<div id="masthead">
34<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
35<tr>
Derek Allard60ca9b72007-07-12 19:53:27 +000036<td><h1>CodeIgniter User Guide Version 1.5.4</h1></td>
adminc0d5d522006-10-30 19:40:35 +000037<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminb0dd10f2006-08-25 17:25:49 +000038</tr>
39</table>
40</div>
41<!-- END NAVIGATION -->
42
43
44<!-- START BREADCRUMB -->
45<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
46<tr>
47<td id="breadcrumb">
Derek Allardd2df9bc2007-04-15 17:41:17 +000048<a href="http://www.codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminb0dd10f2006-08-25 17:25:49 +000049<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50File Uploading Class
51</td>
Derek Allardbc030912007-06-24 18:25:29 +000052<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
adminb0dd10f2006-08-25 17:25:49 +000053</tr>
54</table>
55<!-- END BREADCRUMB -->
56
57<br clear="all" />
58
59
60<!-- START CONTENT -->
61<div id="content">
62
63
64<h1>File Uploading Class</h1>
65
Derek Allardd2df9bc2007-04-15 17:41:17 +000066<p>CodeIgniter's File Uploading Class permits files to be uploaded. You can set various
adminb0dd10f2006-08-25 17:25:49 +000067preferences, restricting the type and size of the files.</p>
68
69
70<h2>The Process</h2>
71
72<p>Uploading a file involves the following general process:</p>
73
74
75<ul>
76<li>An upload form is displayed, allowing a user to select a file and upload it.</li>
77<li>When the form is submitted, the file is uploaded to the destination you specify.</li>
78<li>Along the way, the file is validated to make sure it is allowed to be uploaded based on the preferences you set.</li>
79<li>Once uploaded, the user will be shown a success message.</li>
80</ul>
81
82<p>To demonstrate this process here is brief tutorial. Afterward you'll find reference information.</p>
83
84<h2>Creating the Upload Form</h2>
85
86
87
88<p>Using a text editor, create a form called <dfn>upload_form.php</dfn>. In it, place this code and save it to your <samp>applications/views/</samp>
89folder:</p>
90
91
Derek Allardc6441282007-07-04 23:54:32 +000092<textarea class="textarea" style="width:100%" cols="50" rows="23">
93&lt;html>
94&lt;head>
95&lt;title>Upload Form&lt;/title>
96&lt;/head>
97&lt;body>
adminb0dd10f2006-08-25 17:25:49 +000098
Derek Allardc6441282007-07-04 23:54:32 +000099&lt;?=$error;?>
adminb0dd10f2006-08-25 17:25:49 +0000100
Derek Allardc6441282007-07-04 23:54:32 +0000101&lt;?=form_open_multipart('upload/do_upload'); ?>
adminb0dd10f2006-08-25 17:25:49 +0000102
Derek Allardc6441282007-07-04 23:54:32 +0000103&lt;input type="file" name="userfile" size="20" />
adminb0dd10f2006-08-25 17:25:49 +0000104
Derek Allardc6441282007-07-04 23:54:32 +0000105&lt;br />&lt;br />
adminb0dd10f2006-08-25 17:25:49 +0000106
Derek Allardc6441282007-07-04 23:54:32 +0000107&lt;input type="submit" value="upload" />
adminb0dd10f2006-08-25 17:25:49 +0000108
Derek Allardc6441282007-07-04 23:54:32 +0000109&lt;/form>
adminb0dd10f2006-08-25 17:25:49 +0000110
Derek Allardc6441282007-07-04 23:54:32 +0000111&lt;/body>
112&lt;/html></textarea>
adminb0dd10f2006-08-25 17:25:49 +0000113
admine334c472006-10-21 19:44:22 +0000114<p>You'll notice we are using a form helper to create the opening form tag. File uploads require a multipart form, so the helper
adminb0dd10f2006-08-25 17:25:49 +0000115creates the proper syntax for you. You'll also notice we have an $error variable. This is so we can show error messages in the event
116the user does something wrong.</p>
117
118
119<h2>The Success Page</h2>
120
121<p>Using a text editor, create a form called <dfn>upload_success.php</dfn>.
122In it, place this code and save it to your <samp>applications/views/</samp> folder:</p>
123
Derek Allardc6441282007-07-04 23:54:32 +0000124<textarea class="textarea" style="width:100%" cols="50" rows="20">&lt;html>
125&lt;head>
126&lt;title>Upload Form&lt;/title>
127&lt;/head>
128&lt;body>
adminb0dd10f2006-08-25 17:25:49 +0000129
Derek Allardc6441282007-07-04 23:54:32 +0000130&lt;h3>Your file was successfully uploaded!&lt;/h3>
adminb0dd10f2006-08-25 17:25:49 +0000131
Derek Allardc6441282007-07-04 23:54:32 +0000132&lt;ul>
133&lt;?php foreach($upload_data as $item => $value):?>
134&lt;li>&lt;?=$item;?>: &lt;?=$value;?>&lt;/li>
135&lt;?php endforeach; ?>
136&lt;/ul>
adminb0dd10f2006-08-25 17:25:49 +0000137
Derek Allardc6441282007-07-04 23:54:32 +0000138&lt;p>&lt;?=anchor('upload', 'Upload Another File!'); ?>&lt;/p>
adminb0dd10f2006-08-25 17:25:49 +0000139
Derek Allardc6441282007-07-04 23:54:32 +0000140&lt;/body>
141&lt;/html></textarea>
adminb0dd10f2006-08-25 17:25:49 +0000142
143
144<h2>The Controller</h2>
145
146<p>Using a text editor, create a controller called <dfn>upload.php</dfn>. In it, place this code and save it to your <samp>applications/controllers/</samp>
147folder:</p>
148
149
150<textarea class="textarea" style="width:100%" cols="50" rows="43"><?php
151
152class Upload extends Controller {
153
154 function Upload()
155 {
156 parent::Controller();
157 $this->load->helper(array('form', 'url'));
158 }
159
160 function index()
161 {
162 $this->load->view('upload_form', array('error' => ' ' ));
163 }
164
165 function do_upload()
166 {
admine334c472006-10-21 19:44:22 +0000167 $config['upload_path'] = './uploads/';
adminb0dd10f2006-08-25 17:25:49 +0000168 $config['allowed_types'] = 'gif|jpg|png';
169 $config['max_size'] = '100';
170 $config['max_width'] = '1024';
171 $config['max_height'] = '768';
172
adminb93464d2006-10-31 00:36:32 +0000173 $this->load->library('upload', $config);
adminb0dd10f2006-08-25 17:25:49 +0000174
175 if ( ! $this->upload->do_upload())
176 {
177 $error = array('error' => $this->upload->display_errors());
178
179 $this->load->view('upload_form', $error);
180 }
181 else
182 {
183 $data = array('upload_data' => $this->upload->data());
184
185 $this->load->view('upload_success', $data);
186 }
187 }
188}
189?></textarea>
190
191
192<h2>The Upload Folder</h2>
193
Derek Allardd2df9bc2007-04-15 17:41:17 +0000194<p>You'll need a destination folder for your uploaded images. Create a folder at the root of your CodeIgniter installation called
adminb0dd10f2006-08-25 17:25:49 +0000195<dfn>uploads</dfn> and set its file permissions to 777.</p>
196
197
198<h2>Try it!</h2>
199
200<p>To try your form, visit your site using a URL similar to this one:</p>
201
202<code>www.your-site.com/index.php/<var>upload</var>/</code>
203
admine334c472006-10-21 19:44:22 +0000204<p>You should see an upload form. Try uploading an image file (either a jpg, gif, or png). If the path in your
adminb0dd10f2006-08-25 17:25:49 +0000205controller is correct it should work.</p>
206
207
208<p>&nbsp;</p>
209
210<h1>Reference Guide</h1>
211
212
213<h2>Initializing the Upload Class</h2>
214
Derek Allardd2df9bc2007-04-15 17:41:17 +0000215<p>Like most other classes in CodeIgniter, the Upload class is initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>
adminb0dd10f2006-08-25 17:25:49 +0000216
217<code>$this->load->library('upload');</code>
218<p>Once the Upload class is loaded, the object will be available using: <dfn>$this->upload</dfn></p>
219
220
221<h2>Setting Preferences</h2>
222
admine334c472006-10-21 19:44:22 +0000223<p>Similar to other libraries, you'll control what is allowed to be upload based on your preferences. In the controller you
adminb0dd10f2006-08-25 17:25:49 +0000224built above you set the following preferences:</p>
225
226<code>$config['upload_path'] = './uploads/';<br />
227$config['allowed_types'] = 'gif|jpg|png';<br />
228$config['max_size'] = '100';<br />
229$config['max_width'] = '1024';<br />
230$config['max_height'] = '768';<br />
231<br />
Rick Ellis325197e2006-11-20 17:29:05 +0000232$this->load->library('upload', $config);<br /><br />
adminb93464d2006-10-31 00:36:32 +0000233
234// Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:<br />
adminb0dd10f2006-08-25 17:25:49 +0000235$this->upload->initialize($config);</code>
236
237<p>The above preferences should be fairly self-explanatory. Below is a table describing all available preferences.</p>
238
239
240<h2>Preferences</h2>
241
242<p>The following preferences are available. The default value indicates what will be used if you do not specify that preference.</p>
243
244<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
245<tr>
246<th>Preference</th>
247<th>Default&nbsp;Value</th>
248<th>Options</th>
249<th>Description</th>
250</tr>
251
252<tr>
253<td class="td"><strong>upload_path</strong></td>
254<td class="td">None</td>
255<td class="td">None</td>
256<td class="td">The path to the folder where the upload should be placed. The folder must be writable and the path can be absolute or relative.</td>
257</tr>
258
259<tr>
260<td class="td"><strong>allowed_types</strong></td>
261<td class="td">None</td>
262<td class="td">None</td>
263<td class="td">The mime types corresponding to the types of files you allow to be uploaded. Usually the file extension can be used as the mime type. Separate multiple types with a pipe.</td>
264</tr>
265
266
267<tr>
268<td class="td"><strong>overwrite</strong></td>
269<td class="td">FALSE</td>
270<td class="td">TRUE/FALSE (boolean)</td>
271<td class="td">If set to true, if a file with the same name as the one you are uploading exists, it will be overwritten. If set to false, a number will be appended to the filename if another with the same name exists.</td>
272</tr>
273
274
275<tr>
276<td class="td"><strong>max_size</strong></td>
277<td class="td">0</td>
278<td class="td">None</td>
279<td class="td">The maximum size (in kilobytes) that the file can be. Set to zero for no limit. Note: Most PHP installations have their own limit, as specified in the php.ini file. Usually 2 MB (or 2048 KB) by default.</td>
280</tr>
281
282<tr>
283<td class="td"><strong>max_width</strong></td>
284<td class="td">0</td>
285<td class="td">None</td>
286<td class="td">The maximum width (in pixels) that the file can be. Set to zero for no limit.</td>
287</tr>
288
289<tr>
290<td class="td"><strong>max_height</strong></td>
291<td class="td">0</td>
292<td class="td">None</td>
293<td class="td">The maximum height (in pixels) that the file can be. Set to zero for no limit.</td>
294</tr>
295
296
297<tr>
298<td class="td"><strong>encrypt_name</strong></td>
299<td class="td">FALSE</td>
300<td class="td">TRUE/FALSE (boolean)</td>
301<td class="td">If set to TRUE the file name will be converted to a random encrypted string. This can be useful if you would like the file saved with a name that can not be discerned by the person uploading it.</td>
302</tr>
303
304<tr>
305<td class="td"><strong>remove_spaces</strong></td>
306<td class="td">TRUE</td>
307<td class="td">TRUE/FALSE (boolean)</td>
308<td class="td">If set to TRUE, any spaces in the file name will be converted to underscores. This is recommended.</td>
309</tr>
310</table>
311
312
313<h2>Setting preferences in a config file</h2>
314
315<p>If you prefer not to set preferences using the above method, you can instead put them into a config file.
316Simply create a new file called the <var>upload.php</var>, add the <var>$config</var>
317array in that file. Then save the file in: <var>config/upload.php</var> and it will be used automatically. You
318will NOT need to use the <dfn>$this->upload->initialize</dfn> function if you save your preferences in a config file.</p>
319
320
321<h2>Function Reference</h2>
322
323<p>The following functions are available</p>
324
325
326<h2>$this->upload->do_upload()</h2>
327
admine334c472006-10-21 19:44:22 +0000328<p>Performs the upload based on the preferences you've set. Note: By default the upload routine expects the file to come from a form field
adminb0dd10f2006-08-25 17:25:49 +0000329called <dfn>userfile</dfn>, and the form must be a "multipart type:</p>
330
331<code>&lt;form method="post" action="some_action" enctype="multipart/form-data" /></code>
332
adminddd0c7d2006-09-05 04:05:33 +0000333<p>If you would like to set your own field name simply pass its value to the <dfn>do_upload</dfn> function:</p>
334
335<code>
336$field_name = "some_field_name";<br />
337$this->upload->do_upload($field_name)</code>
338
adminb0dd10f2006-08-25 17:25:49 +0000339
340<h2>$this->upload->display_errors()</h2>
341
342<p>Retrieves any error messages if the <dfn>do_upload()</dfn> function returned false. The function does not echo automatically, it
343returns the data so you can assign it however you need.</p>
344
345<h3>Formatting Errors</h3>
346<p>By default the above function wraps any errors within &lt;p> tags. You can set your own delimiters like this:</p>
347
348<code>$this->upload->display_errors('<var>&lt;p></var>', '<var>&lt;/p></var>');</code>
349
350<h2>$this->upload->data()</h2>
351
admine334c472006-10-21 19:44:22 +0000352<p>This is a helper function that returns an array containing all of the data related to the file you uploaded.
adminb0dd10f2006-08-25 17:25:49 +0000353Here is the array prototype:</p>
354
355<code>Array<br />
356(<br />
357&nbsp;&nbsp;&nbsp;&nbsp;[file_name]&nbsp;&nbsp;&nbsp;&nbsp;=> mypic.jpg<br />
358&nbsp;&nbsp;&nbsp;&nbsp;[file_type]&nbsp;&nbsp;&nbsp;&nbsp;=> image/jpeg<br />
359&nbsp;&nbsp;&nbsp;&nbsp;[file_path]&nbsp;&nbsp;&nbsp;&nbsp;=> /path/to/your/upload/<br />
360&nbsp;&nbsp;&nbsp;&nbsp;[full_path]&nbsp;&nbsp;&nbsp;&nbsp;=> /path/to/your/upload/jpg.jpg<br />
361&nbsp;&nbsp;&nbsp;&nbsp;[raw_name]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> mypic<br />
362&nbsp;&nbsp;&nbsp;&nbsp;[orig_name]&nbsp;&nbsp;&nbsp;&nbsp;=> mypic.jpg<br />
363&nbsp;&nbsp;&nbsp;&nbsp;[file_ext]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> .jpg<br />
364&nbsp;&nbsp;&nbsp;&nbsp;[file_size]&nbsp;&nbsp;&nbsp;&nbsp;=> 22.2<br />
365&nbsp;&nbsp;&nbsp;&nbsp;[is_image]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> 1<br />
366&nbsp;&nbsp;&nbsp;&nbsp;[image_width]&nbsp;&nbsp;=> 800<br />
367&nbsp;&nbsp;&nbsp;&nbsp;[image_height] => 600<br />
368&nbsp;&nbsp;&nbsp;&nbsp;[image_type]&nbsp;&nbsp;&nbsp;=> jpeg<br />
369&nbsp;&nbsp;&nbsp;&nbsp;[image_size_str] => width="800" height="200"<br />
370)</code>
371
372<h3>Explanation</h3>
373
374<p>Here is an explanation of the above array items.</p>
375
376<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
377<tr><th>Item</th><th>Description</th></tr>
378
379<tr><td class="td"><strong>file_name</strong></td>
380<td class="td">The name of the file that was uploaded including the file extension.</td></tr>
381
382<tr><td class="td"><strong>file_type</strong></td>
383<td class="td">The file's Mime type</td></tr>
384
385<tr><td class="td"><strong>file_path</strong></td>
386<td class="td">The absolute server path to the file</td></tr>
387
388<tr><td class="td"><strong>full_path</strong></td>
389<td class="td">The absolute server path including the file name</td></tr>
390
391<tr><td class="td"><strong>raw_name</strong></td>
392<td class="td">The file name without the extension</td></tr>
393
394<tr><td class="td"><strong>orig_name</strong></td>
395<td class="td">The original file name. This is only useful if you use the encrypted name option.</td></tr>
396
397<tr><td class="td"><strong>file_ext</strong></td>
398<td class="td">The file extension with period</td></tr>
399
400<tr><td class="td"><strong>file_size</strong></td>
401<td class="td">The file size in kilobytes</td></tr>
402
403<tr><td class="td"><strong>is_image</strong></td>
404<td class="td">Whether the file is an image or not. 1 = image. 0 = not.</td></tr>
405
406<tr><td class="td"><strong>image_width</strong></td>
407<td class="td">Image width.</td></tr>
408
409<tr><td class="td"><strong>image_heigth</strong></td>
410<td class="td">Image height</td></tr>
411
412<tr><td class="td"><strong>image_type</strong></td>
413<td class="td">Image type. Typically the file extension without the period.</td></tr>
414
415<tr><td class="td"><strong>image_size_str</strong></td>
416<td class="td">A string containing the width and height. Useful to put into an image tag.</td></tr>
417
418
419</table>
420
421</div>
422<!-- END CONTENT -->
423
424
425<div id="footer">
426<p>
Derek Allard9da4dbc2007-04-03 11:39:35 +0000427Previous Topic:&nbsp;&nbsp;<a href="download_helper.html">Download Helper</a>
adminb0dd10f2006-08-25 17:25:49 +0000428&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
429<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
430<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admin3f643e62006-10-27 06:25:31 +0000431Next Topic:&nbsp;&nbsp;<a href="ftp.html">FTP Class</a>
Derek Allardc6441282007-07-04 23:54:32 +0000432</p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000433<p><a href="http://www.codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2007 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminb0dd10f2006-08-25 17:25:49 +0000434</div>
435
436</body>
437</html>