blob: 51dd0d5e2b7c212bbc399c72e238512f5e0c43a1 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001####################
2File Uploading Class
3####################
4
5CodeIgniter's File Uploading Class permits files to be uploaded. You can
6set various preferences, restricting the type and size of the files.
7
8***********
9The Process
10***********
11
12Uploading a file involves the following general process:
13
14- An upload form is displayed, allowing a user to select a file and
15 upload it.
16- When the form is submitted, the file is uploaded to the destination
17 you specify.
18- Along the way, the file is validated to make sure it is allowed to be
19 uploaded based on the preferences you set.
20- Once uploaded, the user will be shown a success message.
21
22To demonstrate this process here is brief tutorial. Afterward you'll
23find reference information.
24
25Creating the Upload Form
26========================
27
28Using a text editor, create a form called upload_form.php. In it, place
Derek Jones07862512011-10-05 16:10:33 -050029this code and save it to your applications/views/ folder::
Derek Jones8ede1a22011-10-05 13:34:52 -050030
Derek Jones07862512011-10-05 16:10:33 -050031 <html>
32 <head>
33 <title>Upload Form</title>
34 </head>
35 <body>
36
37 <?php echo $error;?>
38
39 <?php echo form_open_multipart('upload/do_upload');?>
40
41 <input type="file" name="userfile" size="20" />
42
43 <br /><br />
44
45 <input type="submit" value="upload" />
46
47 </form>
48
49 </body>
50 </html>
51
Derek Jones8ede1a22011-10-05 13:34:52 -050052You'll notice we are using a form helper to create the opening form tag.
53File uploads require a multipart form, so the helper creates the proper
54syntax for you. You'll also notice we have an $error variable. This is
55so we can show error messages in the event the user does something
56wrong.
57
58The Success Page
59================
60
61Using a text editor, create a form called upload_success.php. In it,
Derek Jones07862512011-10-05 16:10:33 -050062place this code and save it to your applications/views/ folder::
Derek Jones8ede1a22011-10-05 13:34:52 -050063
Derek Jones07862512011-10-05 16:10:33 -050064 <html>
65 <head>
66 <title>Upload Form</title>
67 </head>
68 <body>
69
70 <h3>Your file was successfully uploaded!</h3>
71
72 <ul>
73 <?php foreach ($upload_data as $item => $value):?>
74 <li><?php echo $item;?>: <?php echo $value;?></li>
75 <?php endforeach; ?>
76 </ul>
77
78 <p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
79
80 </body>
81 </html>
82
Derek Jones8ede1a22011-10-05 13:34:52 -050083The Controller
84==============
85
86Using a text editor, create a controller called upload.php. In it, place
Derek Jones07862512011-10-05 16:10:33 -050087this code and save it to your applications/controllers/ folder::
Derek Jones8ede1a22011-10-05 13:34:52 -050088
Derek Jones07862512011-10-05 16:10:33 -050089 <?php
90
91 class Upload extends CI_Controller {
92
93 function __construct()
94 {
95 parent::__construct();
96 $this->load->helper(array('form', 'url'));
97 }
98
99 function index()
100 {
101 $this->load->view('upload_form', array('error' => ' ' ));
102 }
103
104 function do_upload()
105 {
106 $config['upload_path'] = './uploads/';
107 $config['allowed_types'] = 'gif|jpg|png';
108 $config['max_size'] = '100';
109 $config['max_width'] = '1024';
110 $config['max_height'] = '768';
111
112 $this->load->library('upload', $config);
113
114 if ( ! $this->upload->do_upload())
115 {
116 $error = array('error' => $this->upload->display_errors());
117
118 $this->load->view('upload_form', $error);
119 }
120 else
121 {
122 $data = array('upload_data' => $this->upload->data());
123
124 $this->load->view('upload_success', $data);
125 }
126 }
127 }
128 ?>
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
130The Upload Folder
131=================
132
133You'll need a destination folder for your uploaded images. Create a
134folder at the root of your CodeIgniter installation called uploads and
135set its file permissions to 777.
136
137Try it!
138=======
139
140To try your form, visit your site using a URL similar to this one::
141
142 example.com/index.php/upload/
143
144You should see an upload form. Try uploading an image file (either a
145jpg, gif, or png). If the path in your controller is correct it should
146work.
147
148***************
149Reference Guide
150***************
151
152Initializing the Upload Class
153=============================
154
155Like most other classes in CodeIgniter, the Upload class is initialized
156in your controller using the $this->load->library function::
157
158 $this->load->library('upload');
159
160Once the Upload class is loaded, the object will be available using:
161$this->upload
162
163Setting Preferences
164===================
165
166Similar to other libraries, you'll control what is allowed to be upload
167based on your preferences. In the controller you built above you set the
168following preferences::
169
Derek Jones07862512011-10-05 16:10:33 -0500170 $config['upload_path'] = './uploads/';
171 $config['allowed_types'] = 'gif|jpg|png';
172 $config['max_size'] = '100';
173 $config['max_width'] = '1024';
174 $config['max_height'] = '768';
175
176 $this->load->library('upload', $config);
177
178 // Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:
179 $this->upload->initialize($config);
Derek Jones8ede1a22011-10-05 13:34:52 -0500180
181The above preferences should be fairly self-explanatory. Below is a
182table describing all available preferences.
183
184Preferences
185===========
186
187The following preferences are available. The default value indicates
188what will be used if you do not specify that preference.
189
190Preference
191Default Value
192Options
193Description
194**upload_path**
195None
196None
197The path to the folder where the upload should be placed. The folder
198must be writable and the path can be absolute or relative.
199**allowed_types**
200None
201None
202The mime types corresponding to the types of files you allow to be
203uploaded. Usually the file extension can be used as the mime type.
204Separate multiple types with a pipe.
205**file_name**
206None
207Desired file name
208If set CodeIgniter will rename the uploaded file to this name. The
209extension provided in the file name must also be an allowed file type.
210
211**overwrite**
212FALSE
213TRUE/FALSE (boolean)
214If set to true, if a file with the same name as the one you are
215uploading exists, it will be overwritten. If set to false, a number will
216be appended to the filename if another with the same name exists.
217**max_size**
2180
219None
220The maximum size (in kilobytes) that the file can be. Set to zero for no
221limit. Note: Most PHP installations have their own limit, as specified
222in the php.ini file. Usually 2 MB (or 2048 KB) by default.
223**max_width**
2240
225None
226The maximum width (in pixels) that the file can be. Set to zero for no
227limit.
228**max_height**
2290
230None
231The maximum height (in pixels) that the file can be. Set to zero for no
232limit.
233**max_filename**
2340
235None
236The maximum length that a file name can be. Set to zero for no limit.
237**max_filename_increment**
238100
239None
240When overwrite is set to FALSE, use this to set the maximum filename
241increment for CodeIgniter to append to the filename.
242**encrypt_name**
243FALSE
244TRUE/FALSE (boolean)
245If set to TRUE the file name will be converted to a random encrypted
246string. This can be useful if you would like the file saved with a name
247that can not be discerned by the person uploading it.
248**remove_spaces**
249TRUE
250TRUE/FALSE (boolean)
251If set to TRUE, any spaces in the file name will be converted to
252underscores. This is recommended.
253
254Setting preferences in a config file
255====================================
256
257If you prefer not to set preferences using the above method, you can
258instead put them into a config file. Simply create a new file called the
259upload.php, add the $config array in that file. Then save the file in:
260config/upload.php and it will be used automatically. You will NOT need
261to use the $this->upload->initialize function if you save your
262preferences in a config file.
263
264******************
265Function Reference
266******************
267
268The following functions are available
269
270$this->upload->do_upload()
271===========================
272
273Performs the upload based on the preferences you've set. Note: By
274default the upload routine expects the file to come from a form field
275called userfile, and the form must be a "multipart type::
276
277 <form method="post" action="some_action" enctype="multipart/form-data" />
278
279If you would like to set your own field name simply pass its value to
280the do_upload function::
281
Derek Jones07862512011-10-05 16:10:33 -0500282 $field_name = "some_field_name";
283 $this->upload->do_upload($field_name);
Derek Jones8ede1a22011-10-05 13:34:52 -0500284
285$this->upload->display_errors()
286================================
287
288Retrieves any error messages if the do_upload() function returned
289false. The function does not echo automatically, it returns the data so
290you can assign it however you need.
291
292Formatting Errors
293*****************
294
295By default the above function wraps any errors within <p> tags. You can
296set your own delimiters like this::
297
298 $this->upload->display_errors('<p>', '</p>');
299
300$this->upload->data()
301=====================
302
303This is a helper function that returns an array containing all of the
304data related to the file you uploaded. Here is the array prototype::
305
Derek Jones07862512011-10-05 16:10:33 -0500306 Array
307 (
308 [file_name] => mypic.jpg
309 [file_type] => image/jpeg
310 [file_path] => /path/to/your/upload/
311 [full_path] => /path/to/your/upload/jpg.jpg
312 [raw_name] => mypic
313 [orig_name] => mypic.jpg
314 [client_name] => mypic.jpg
315 [file_ext] => .jpg
316 [file_size] => 22.2
317 [is_image] => 1
318 [image_width] => 800
319 [image_height] => 600
320 [image_type] => jpeg
321 [image_size_str] => width="800" height="200"
322 )
Derek Jones8ede1a22011-10-05 13:34:52 -0500323
324Explanation
325***********
326
327Here is an explanation of the above array items.
328
329Item
330Description
331**file_name**
332The name of the file that was uploaded including the file extension.
333**file_type**
334The file's Mime type
335**file_path**
336The absolute server path to the file
337**full_path**
338The absolute server path including the file name
339**raw_name**
340The file name without the extension
341**orig_name**
342The original file name. This is only useful if you use the encrypted
343name option.
344**client_name**
345The file name as supplied by the client user agent, prior to any file
346name preparation or incrementing.
347**file_ext**
348The file extension with period
349**file_size**
350The file size in kilobytes
351**is_image**
352Whether the file is an image or not. 1 = image. 0 = not.
353**image_width**
354Image width.
355**image_height**
356Image height
357**image_type**
358Image type. Typically the file extension without the period.
359**image_size_str**
360A string containing the width and height. Useful to put into an image
361tag.