blob: 01f9b6851f52ce22aaeae02aab0ebceb15ffce21 [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
5<title>Code Igniter User Guide</title>
6
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' />
25<meta name='description' content='Code Igniter User Guide' />
26
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 Allardf743c732007-02-14 01:36:46 +000036<td><h1>Code Igniter User Guide Version 1.5.2</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">
48<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
49<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50File Uploading Class
51</td>
52<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.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>
53</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
admine334c472006-10-21 19:44:22 +000066<p>Code Igniter'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
92<textarea class="textarea" style="width:100%" cols="50" rows="23"><html>
93<head>
94<title>Upload Form</title>
95</head>
96<body>
97
98<?=$error;?>
99
100<?=form_open_multipart('upload/do_upload'); ?>
101
102<input type="file" name="userfile" size="20" />
103
104<br /><br />
105
106<input type="submit" value="upload" />
107
108</form>
109
110</body>
111</html></textarea>
112
admine334c472006-10-21 19:44:22 +0000113<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 +0000114creates 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
115the user does something wrong.</p>
116
117
118<h2>The Success Page</h2>
119
120<p>Using a text editor, create a form called <dfn>upload_success.php</dfn>.
121In it, place this code and save it to your <samp>applications/views/</samp> folder:</p>
122
123<textarea class="textarea" style="width:100%" cols="50" rows="20"><html>
124<head>
125<title>Upload Form</title>
126</head>
127<body>
128
129<h3>Your file was successfully uploaded!</h3>
130
131<ul>
132<?php foreach($upload_data as $item => $value):?>
133<li><?=$item;?>: <?=$value;?></li>
134<?php endforeach; ?>
135</ul>
136
137<p><?=anchor('upload', 'Upload Another File!'); ?></p>
138
139</body>
140</html></textarea>
141
142
143<h2>The Controller</h2>
144
145<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>
146folder:</p>
147
148
149<textarea class="textarea" style="width:100%" cols="50" rows="43"><?php
150
151class Upload extends Controller {
152
153 function Upload()
154 {
155 parent::Controller();
156 $this->load->helper(array('form', 'url'));
157 }
158
159 function index()
160 {
161 $this->load->view('upload_form', array('error' => ' ' ));
162 }
163
164 function do_upload()
165 {
admine334c472006-10-21 19:44:22 +0000166 $config['upload_path'] = './uploads/';
adminb0dd10f2006-08-25 17:25:49 +0000167 $config['allowed_types'] = 'gif|jpg|png';
168 $config['max_size'] = '100';
169 $config['max_width'] = '1024';
170 $config['max_height'] = '768';
171
adminb93464d2006-10-31 00:36:32 +0000172 $this->load->library('upload', $config);
adminb0dd10f2006-08-25 17:25:49 +0000173
174 if ( ! $this->upload->do_upload())
175 {
176 $error = array('error' => $this->upload->display_errors());
177
178 $this->load->view('upload_form', $error);
179 }
180 else
181 {
182 $data = array('upload_data' => $this->upload->data());
183
184 $this->load->view('upload_success', $data);
185 }
186 }
187}
188?></textarea>
189
190
191<h2>The Upload Folder</h2>
192
193<p>You'll need a destination folder for your uploaded images. Create a folder at the root of your Code Igniter installation called
194<dfn>uploads</dfn> and set its file permissions to 777.</p>
195
196
197<h2>Try it!</h2>
198
199<p>To try your form, visit your site using a URL similar to this one:</p>
200
201<code>www.your-site.com/index.php/<var>upload</var>/</code>
202
admine334c472006-10-21 19:44:22 +0000203<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 +0000204controller is correct it should work.</p>
205
206
207<p>&nbsp;</p>
208
209<h1>Reference Guide</h1>
210
211
212<h2>Initializing the Upload Class</h2>
213
214<p>Like most other classes in Code Igniter, the Upload class is initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>
215
216<code>$this->load->library('upload');</code>
217<p>Once the Upload class is loaded, the object will be available using: <dfn>$this->upload</dfn></p>
218
219
220<h2>Setting Preferences</h2>
221
admine334c472006-10-21 19:44:22 +0000222<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 +0000223built above you set the following preferences:</p>
224
225<code>$config['upload_path'] = './uploads/';<br />
226$config['allowed_types'] = 'gif|jpg|png';<br />
227$config['max_size'] = '100';<br />
228$config['max_width'] = '1024';<br />
229$config['max_height'] = '768';<br />
230<br />
Rick Ellis325197e2006-11-20 17:29:05 +0000231$this->load->library('upload', $config);<br /><br />
adminb93464d2006-10-31 00:36:32 +0000232
233// Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:<br />
adminb0dd10f2006-08-25 17:25:49 +0000234$this->upload->initialize($config);</code>
235
236<p>The above preferences should be fairly self-explanatory. Below is a table describing all available preferences.</p>
237
238
239<h2>Preferences</h2>
240
241<p>The following preferences are available. The default value indicates what will be used if you do not specify that preference.</p>
242
243<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
244<tr>
245<th>Preference</th>
246<th>Default&nbsp;Value</th>
247<th>Options</th>
248<th>Description</th>
249</tr>
250
251<tr>
252<td class="td"><strong>upload_path</strong></td>
253<td class="td">None</td>
254<td class="td">None</td>
255<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>
256</tr>
257
258<tr>
259<td class="td"><strong>allowed_types</strong></td>
260<td class="td">None</td>
261<td class="td">None</td>
262<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>
263</tr>
264
265
266<tr>
267<td class="td"><strong>overwrite</strong></td>
268<td class="td">FALSE</td>
269<td class="td">TRUE/FALSE (boolean)</td>
270<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>
271</tr>
272
273
274<tr>
275<td class="td"><strong>max_size</strong></td>
276<td class="td">0</td>
277<td class="td">None</td>
278<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>
279</tr>
280
281<tr>
282<td class="td"><strong>max_width</strong></td>
283<td class="td">0</td>
284<td class="td">None</td>
285<td class="td">The maximum width (in pixels) that the file can be. Set to zero for no limit.</td>
286</tr>
287
288<tr>
289<td class="td"><strong>max_height</strong></td>
290<td class="td">0</td>
291<td class="td">None</td>
292<td class="td">The maximum height (in pixels) that the file can be. Set to zero for no limit.</td>
293</tr>
294
295
296<tr>
297<td class="td"><strong>encrypt_name</strong></td>
298<td class="td">FALSE</td>
299<td class="td">TRUE/FALSE (boolean)</td>
300<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>
301</tr>
302
303<tr>
304<td class="td"><strong>remove_spaces</strong></td>
305<td class="td">TRUE</td>
306<td class="td">TRUE/FALSE (boolean)</td>
307<td class="td">If set to TRUE, any spaces in the file name will be converted to underscores. This is recommended.</td>
308</tr>
309</table>
310
311
312<h2>Setting preferences in a config file</h2>
313
314<p>If you prefer not to set preferences using the above method, you can instead put them into a config file.
315Simply create a new file called the <var>upload.php</var>, add the <var>$config</var>
316array in that file. Then save the file in: <var>config/upload.php</var> and it will be used automatically. You
317will NOT need to use the <dfn>$this->upload->initialize</dfn> function if you save your preferences in a config file.</p>
318
319
320<h2>Function Reference</h2>
321
322<p>The following functions are available</p>
323
324
325<h2>$this->upload->do_upload()</h2>
326
admine334c472006-10-21 19:44:22 +0000327<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 +0000328called <dfn>userfile</dfn>, and the form must be a "multipart type:</p>
329
330<code>&lt;form method="post" action="some_action" enctype="multipart/form-data" /></code>
331
adminddd0c7d2006-09-05 04:05:33 +0000332<p>If you would like to set your own field name simply pass its value to the <dfn>do_upload</dfn> function:</p>
333
334<code>
335$field_name = "some_field_name";<br />
336$this->upload->do_upload($field_name)</code>
337
adminb0dd10f2006-08-25 17:25:49 +0000338
339<h2>$this->upload->display_errors()</h2>
340
341<p>Retrieves any error messages if the <dfn>do_upload()</dfn> function returned false. The function does not echo automatically, it
342returns the data so you can assign it however you need.</p>
343
344<h3>Formatting Errors</h3>
345<p>By default the above function wraps any errors within &lt;p> tags. You can set your own delimiters like this:</p>
346
347<code>$this->upload->display_errors('<var>&lt;p></var>', '<var>&lt;/p></var>');</code>
348
349<h2>$this->upload->data()</h2>
350
admine334c472006-10-21 19:44:22 +0000351<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 +0000352Here is the array prototype:</p>
353
354<code>Array<br />
355(<br />
356&nbsp;&nbsp;&nbsp;&nbsp;[file_name]&nbsp;&nbsp;&nbsp;&nbsp;=> mypic.jpg<br />
357&nbsp;&nbsp;&nbsp;&nbsp;[file_type]&nbsp;&nbsp;&nbsp;&nbsp;=> image/jpeg<br />
358&nbsp;&nbsp;&nbsp;&nbsp;[file_path]&nbsp;&nbsp;&nbsp;&nbsp;=> /path/to/your/upload/<br />
359&nbsp;&nbsp;&nbsp;&nbsp;[full_path]&nbsp;&nbsp;&nbsp;&nbsp;=> /path/to/your/upload/jpg.jpg<br />
360&nbsp;&nbsp;&nbsp;&nbsp;[raw_name]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> mypic<br />
361&nbsp;&nbsp;&nbsp;&nbsp;[orig_name]&nbsp;&nbsp;&nbsp;&nbsp;=> mypic.jpg<br />
362&nbsp;&nbsp;&nbsp;&nbsp;[file_ext]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> .jpg<br />
363&nbsp;&nbsp;&nbsp;&nbsp;[file_size]&nbsp;&nbsp;&nbsp;&nbsp;=> 22.2<br />
364&nbsp;&nbsp;&nbsp;&nbsp;[is_image]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> 1<br />
365&nbsp;&nbsp;&nbsp;&nbsp;[image_width]&nbsp;&nbsp;=> 800<br />
366&nbsp;&nbsp;&nbsp;&nbsp;[image_height] => 600<br />
367&nbsp;&nbsp;&nbsp;&nbsp;[image_type]&nbsp;&nbsp;&nbsp;=> jpeg<br />
368&nbsp;&nbsp;&nbsp;&nbsp;[image_size_str] => width="800" height="200"<br />
369)</code>
370
371<h3>Explanation</h3>
372
373<p>Here is an explanation of the above array items.</p>
374
375<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
376<tr><th>Item</th><th>Description</th></tr>
377
378<tr><td class="td"><strong>file_name</strong></td>
379<td class="td">The name of the file that was uploaded including the file extension.</td></tr>
380
381<tr><td class="td"><strong>file_type</strong></td>
382<td class="td">The file's Mime type</td></tr>
383
384<tr><td class="td"><strong>file_path</strong></td>
385<td class="td">The absolute server path to the file</td></tr>
386
387<tr><td class="td"><strong>full_path</strong></td>
388<td class="td">The absolute server path including the file name</td></tr>
389
390<tr><td class="td"><strong>raw_name</strong></td>
391<td class="td">The file name without the extension</td></tr>
392
393<tr><td class="td"><strong>orig_name</strong></td>
394<td class="td">The original file name. This is only useful if you use the encrypted name option.</td></tr>
395
396<tr><td class="td"><strong>file_ext</strong></td>
397<td class="td">The file extension with period</td></tr>
398
399<tr><td class="td"><strong>file_size</strong></td>
400<td class="td">The file size in kilobytes</td></tr>
401
402<tr><td class="td"><strong>is_image</strong></td>
403<td class="td">Whether the file is an image or not. 1 = image. 0 = not.</td></tr>
404
405<tr><td class="td"><strong>image_width</strong></td>
406<td class="td">Image width.</td></tr>
407
408<tr><td class="td"><strong>image_heigth</strong></td>
409<td class="td">Image height</td></tr>
410
411<tr><td class="td"><strong>image_type</strong></td>
412<td class="td">Image type. Typically the file extension without the period.</td></tr>
413
414<tr><td class="td"><strong>image_size_str</strong></td>
415<td class="td">A string containing the width and height. Useful to put into an image tag.</td></tr>
416
417
418</table>
419
420</div>
421<!-- END CONTENT -->
422
423
424<div id="footer">
425<p>
426Previous Topic:&nbsp;&nbsp;<a href="encryption.html">Encryption Class</a>
427&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
428<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
429<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
admin3f643e62006-10-27 06:25:31 +0000430Next Topic:&nbsp;&nbsp;<a href="ftp.html">FTP Class</a>
adminb0dd10f2006-08-25 17:25:49 +0000431<p>
432<p><a href="http://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
433</div>
434
435</body>
436</html>