admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <!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 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 10 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 13 | <script type="text/javascript">
|
| 14 | window.onload = function() {
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | myHeight = new fx.Height('nav', {duration: 400});
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 16 | 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>
|
admin | e0cd609 | 2006-10-20 01:00:31 +0000 | [diff] [blame] | 36 | <td><h1>Code Igniter User Guide Version 1.5.0b3</h1></td>
|
admin | 4979a12 | 2006-10-11 19:40:07 +0000 | [diff] [blame] | 37 | <td id="breadcrumb_right"><a href="../toc.html">Linear Table of Contents</a></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 38 | </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> ›
|
| 49 | <a href="../index.html">User Guide Home</a> ›
|
| 50 | File 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 <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <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 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 66 | <p>Code Igniter's File Uploading Class permits files to be uploaded. You can set various
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 67 | preferences, 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>
|
| 89 | folder:</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 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 113 | <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
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 114 | creates 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
|
| 115 | the 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>.
|
| 121 | In 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>
|
| 146 | folder:</p>
|
| 147 |
|
| 148 |
|
| 149 | <textarea class="textarea" style="width:100%" cols="50" rows="43"><?php
|
| 150 |
|
| 151 | class 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 | {
|
| 166 | $this->load->library('upload');
|
| 167 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 168 | $config['upload_path'] = './uploads/';
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 169 | $config['allowed_types'] = 'gif|jpg|png';
|
| 170 | $config['max_size'] = '100';
|
| 171 | $config['max_width'] = '1024';
|
| 172 | $config['max_height'] = '768';
|
| 173 |
|
| 174 | $this->upload->initialize($config);
|
| 175 |
|
| 176 | if ( ! $this->upload->do_upload())
|
| 177 | {
|
| 178 | $error = array('error' => $this->upload->display_errors());
|
| 179 |
|
| 180 | $this->load->view('upload_form', $error);
|
| 181 | }
|
| 182 | else
|
| 183 | {
|
| 184 | $data = array('upload_data' => $this->upload->data());
|
| 185 |
|
| 186 | $this->load->view('upload_success', $data);
|
| 187 | }
|
| 188 | }
|
| 189 | }
|
| 190 | ?></textarea>
|
| 191 |
|
| 192 |
|
| 193 | <h2>The Upload Folder</h2>
|
| 194 |
|
| 195 | <p>You'll need a destination folder for your uploaded images. Create a folder at the root of your Code Igniter installation called
|
| 196 | <dfn>uploads</dfn> and set its file permissions to 777.</p>
|
| 197 |
|
| 198 |
|
| 199 | <h2>Try it!</h2>
|
| 200 |
|
| 201 | <p>To try your form, visit your site using a URL similar to this one:</p>
|
| 202 |
|
| 203 | <code>www.your-site.com/index.php/<var>upload</var>/</code>
|
| 204 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 205 | <p>You should see an upload form. Try uploading an image file (either a jpg, gif, or png). If the path in your
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 206 | controller is correct it should work.</p>
|
| 207 |
|
| 208 |
|
| 209 | <p> </p>
|
| 210 |
|
| 211 | <h1>Reference Guide</h1>
|
| 212 |
|
| 213 |
|
| 214 | <h2>Initializing the Upload Class</h2>
|
| 215 |
|
| 216 | <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>
|
| 217 |
|
| 218 | <code>$this->load->library('upload');</code>
|
| 219 | <p>Once the Upload class is loaded, the object will be available using: <dfn>$this->upload</dfn></p>
|
| 220 |
|
| 221 |
|
| 222 | <h2>Setting Preferences</h2>
|
| 223 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 224 | <p>Similar to other libraries, you'll control what is allowed to be upload based on your preferences. In the controller you
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 225 | built above you set the following preferences:</p>
|
| 226 |
|
| 227 | <code>$config['upload_path'] = './uploads/';<br />
|
| 228 | $config['allowed_types'] = 'gif|jpg|png';<br />
|
| 229 | $config['max_size'] = '100';<br />
|
| 230 | $config['max_width'] = '1024';<br />
|
| 231 | $config['max_height'] = '768';<br />
|
| 232 | <br />
|
| 233 | $this->upload->initialize($config);</code>
|
| 234 |
|
| 235 | <p>The above preferences should be fairly self-explanatory. Below is a table describing all available preferences.</p>
|
| 236 |
|
| 237 |
|
| 238 | <h2>Preferences</h2>
|
| 239 |
|
| 240 | <p>The following preferences are available. The default value indicates what will be used if you do not specify that preference.</p>
|
| 241 |
|
| 242 | <table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
|
| 243 | <tr>
|
| 244 | <th>Preference</th>
|
| 245 | <th>Default Value</th>
|
| 246 | <th>Options</th>
|
| 247 | <th>Description</th>
|
| 248 | </tr>
|
| 249 |
|
| 250 | <tr>
|
| 251 | <td class="td"><strong>upload_path</strong></td>
|
| 252 | <td class="td">None</td>
|
| 253 | <td class="td">None</td>
|
| 254 | <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>
|
| 255 | </tr>
|
| 256 |
|
| 257 | <tr>
|
| 258 | <td class="td"><strong>allowed_types</strong></td>
|
| 259 | <td class="td">None</td>
|
| 260 | <td class="td">None</td>
|
| 261 | <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>
|
| 262 | </tr>
|
| 263 |
|
| 264 |
|
| 265 | <tr>
|
| 266 | <td class="td"><strong>overwrite</strong></td>
|
| 267 | <td class="td">FALSE</td>
|
| 268 | <td class="td">TRUE/FALSE (boolean)</td>
|
| 269 | <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>
|
| 270 | </tr>
|
| 271 |
|
| 272 |
|
| 273 | <tr>
|
| 274 | <td class="td"><strong>max_size</strong></td>
|
| 275 | <td class="td">0</td>
|
| 276 | <td class="td">None</td>
|
| 277 | <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>
|
| 278 | </tr>
|
| 279 |
|
| 280 | <tr>
|
| 281 | <td class="td"><strong>max_width</strong></td>
|
| 282 | <td class="td">0</td>
|
| 283 | <td class="td">None</td>
|
| 284 | <td class="td">The maximum width (in pixels) that the file can be. Set to zero for no limit.</td>
|
| 285 | </tr>
|
| 286 |
|
| 287 | <tr>
|
| 288 | <td class="td"><strong>max_height</strong></td>
|
| 289 | <td class="td">0</td>
|
| 290 | <td class="td">None</td>
|
| 291 | <td class="td">The maximum height (in pixels) that the file can be. Set to zero for no limit.</td>
|
| 292 | </tr>
|
| 293 |
|
| 294 |
|
| 295 | <tr>
|
| 296 | <td class="td"><strong>encrypt_name</strong></td>
|
| 297 | <td class="td">FALSE</td>
|
| 298 | <td class="td">TRUE/FALSE (boolean)</td>
|
| 299 | <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>
|
| 300 | </tr>
|
| 301 |
|
| 302 | <tr>
|
| 303 | <td class="td"><strong>remove_spaces</strong></td>
|
| 304 | <td class="td">TRUE</td>
|
| 305 | <td class="td">TRUE/FALSE (boolean)</td>
|
| 306 | <td class="td">If set to TRUE, any spaces in the file name will be converted to underscores. This is recommended.</td>
|
| 307 | </tr>
|
| 308 | </table>
|
| 309 |
|
| 310 |
|
| 311 | <h2>Setting preferences in a config file</h2>
|
| 312 |
|
| 313 | <p>If you prefer not to set preferences using the above method, you can instead put them into a config file.
|
| 314 | Simply create a new file called the <var>upload.php</var>, add the <var>$config</var>
|
| 315 | array in that file. Then save the file in: <var>config/upload.php</var> and it will be used automatically. You
|
| 316 | will NOT need to use the <dfn>$this->upload->initialize</dfn> function if you save your preferences in a config file.</p>
|
| 317 |
|
| 318 |
|
| 319 | <h2>Function Reference</h2>
|
| 320 |
|
| 321 | <p>The following functions are available</p>
|
| 322 |
|
| 323 |
|
| 324 | <h2>$this->upload->do_upload()</h2>
|
| 325 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 326 | <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
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 327 | called <dfn>userfile</dfn>, and the form must be a "multipart type:</p>
|
| 328 |
|
| 329 | <code><form method="post" action="some_action" enctype="multipart/form-data" /></code>
|
| 330 |
|
admin | ddd0c7d | 2006-09-05 04:05:33 +0000 | [diff] [blame] | 331 | <p>If you would like to set your own field name simply pass its value to the <dfn>do_upload</dfn> function:</p>
|
| 332 |
|
| 333 | <code>
|
| 334 | $field_name = "some_field_name";<br />
|
| 335 | $this->upload->do_upload($field_name)</code>
|
| 336 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 337 |
|
| 338 | <h2>$this->upload->display_errors()</h2>
|
| 339 |
|
| 340 | <p>Retrieves any error messages if the <dfn>do_upload()</dfn> function returned false. The function does not echo automatically, it
|
| 341 | returns the data so you can assign it however you need.</p>
|
| 342 |
|
| 343 | <h3>Formatting Errors</h3>
|
| 344 | <p>By default the above function wraps any errors within <p> tags. You can set your own delimiters like this:</p>
|
| 345 |
|
| 346 | <code>$this->upload->display_errors('<var><p></var>', '<var></p></var>');</code>
|
| 347 |
|
| 348 | <h2>$this->upload->data()</h2>
|
| 349 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 350 | <p>This is a helper function that returns an array containing all of the data related to the file you uploaded.
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 351 | Here is the array prototype:</p>
|
| 352 |
|
| 353 | <code>Array<br />
|
| 354 | (<br />
|
| 355 | [file_name] => mypic.jpg<br />
|
| 356 | [file_type] => image/jpeg<br />
|
| 357 | [file_path] => /path/to/your/upload/<br />
|
| 358 | [full_path] => /path/to/your/upload/jpg.jpg<br />
|
| 359 | [raw_name] => mypic<br />
|
| 360 | [orig_name] => mypic.jpg<br />
|
| 361 | [file_ext] => .jpg<br />
|
| 362 | [file_size] => 22.2<br />
|
| 363 | [is_image] => 1<br />
|
| 364 | [image_width] => 800<br />
|
| 365 | [image_height] => 600<br />
|
| 366 | [image_type] => jpeg<br />
|
| 367 | [image_size_str] => width="800" height="200"<br />
|
| 368 | )</code>
|
| 369 |
|
| 370 | <h3>Explanation</h3>
|
| 371 |
|
| 372 | <p>Here is an explanation of the above array items.</p>
|
| 373 |
|
| 374 | <table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
|
| 375 | <tr><th>Item</th><th>Description</th></tr>
|
| 376 |
|
| 377 | <tr><td class="td"><strong>file_name</strong></td>
|
| 378 | <td class="td">The name of the file that was uploaded including the file extension.</td></tr>
|
| 379 |
|
| 380 | <tr><td class="td"><strong>file_type</strong></td>
|
| 381 | <td class="td">The file's Mime type</td></tr>
|
| 382 |
|
| 383 | <tr><td class="td"><strong>file_path</strong></td>
|
| 384 | <td class="td">The absolute server path to the file</td></tr>
|
| 385 |
|
| 386 | <tr><td class="td"><strong>full_path</strong></td>
|
| 387 | <td class="td">The absolute server path including the file name</td></tr>
|
| 388 |
|
| 389 | <tr><td class="td"><strong>raw_name</strong></td>
|
| 390 | <td class="td">The file name without the extension</td></tr>
|
| 391 |
|
| 392 | <tr><td class="td"><strong>orig_name</strong></td>
|
| 393 | <td class="td">The original file name. This is only useful if you use the encrypted name option.</td></tr>
|
| 394 |
|
| 395 | <tr><td class="td"><strong>file_ext</strong></td>
|
| 396 | <td class="td">The file extension with period</td></tr>
|
| 397 |
|
| 398 | <tr><td class="td"><strong>file_size</strong></td>
|
| 399 | <td class="td">The file size in kilobytes</td></tr>
|
| 400 |
|
| 401 | <tr><td class="td"><strong>is_image</strong></td>
|
| 402 | <td class="td">Whether the file is an image or not. 1 = image. 0 = not.</td></tr>
|
| 403 |
|
| 404 | <tr><td class="td"><strong>image_width</strong></td>
|
| 405 | <td class="td">Image width.</td></tr>
|
| 406 |
|
| 407 | <tr><td class="td"><strong>image_heigth</strong></td>
|
| 408 | <td class="td">Image height</td></tr>
|
| 409 |
|
| 410 | <tr><td class="td"><strong>image_type</strong></td>
|
| 411 | <td class="td">Image type. Typically the file extension without the period.</td></tr>
|
| 412 |
|
| 413 | <tr><td class="td"><strong>image_size_str</strong></td>
|
| 414 | <td class="td">A string containing the width and height. Useful to put into an image tag.</td></tr>
|
| 415 |
|
| 416 |
|
| 417 | </table>
|
| 418 |
|
| 419 | </div>
|
| 420 | <!-- END CONTENT -->
|
| 421 |
|
| 422 |
|
| 423 | <div id="footer">
|
| 424 | <p>
|
| 425 | Previous Topic: <a href="encryption.html">Encryption Class</a>
|
| 426 | ·
|
| 427 | <a href="#top">Top of Page</a> ·
|
| 428 | <a href="../index.html">User Guide Home</a> ·
|
admin | 3f643e6 | 2006-10-27 06:25:31 +0000 | [diff] [blame^] | 429 | Next Topic: <a href="ftp.html">FTP Class</a>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 430 | <p>
|
| 431 | <p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
|
| 432 | </div>
|
| 433 |
|
| 434 | </body>
|
| 435 | </html> |