blob: 583a9769375f4205690621296c4df818d55e6cd6 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev3a459572011-12-21 11:23:11 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev3a459572011-12-21 11:23:11 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * File Uploading Class
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Uploads
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/libraries/file_uploading.html
37 */
38class CI_Upload {
Barry Mienydd671972010-10-04 16:33:58 +020039
Andrey Andreevf5ccd122012-11-02 00:17:39 +020040 /**
41 * Maximum file size
42 *
43 * @var int
44 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020045 public $max_size = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020046
47 /**
48 * Maximum image width
49 *
50 * @var int
51 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020052 public $max_width = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020053
54 /**
55 * Maximum image height
56 *
57 * @var int
58 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020059 public $max_height = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020060
61 /**
Andrey Andreev05aa2d62012-12-03 16:06:55 +020062 * Minimum image width
63 *
64 * @var int
65 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020066 public $min_width = 0;
Andrey Andreev05aa2d62012-12-03 16:06:55 +020067
68 /**
69 * Minimum image height
70 *
71 * @var int
72 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020073 public $min_height = 0;
Andrey Andreev05aa2d62012-12-03 16:06:55 +020074
75 /**
Andrey Andreevf5ccd122012-11-02 00:17:39 +020076 * Maximum filename length
77 *
78 * @var int
79 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020080 public $max_filename = 0;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020081
82 /**
83 * Maximum duplicate filename increment ID
84 *
85 * @var int
86 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020087 public $max_filename_increment = 100;
Andrey Andreevf5ccd122012-11-02 00:17:39 +020088
89 /**
90 * Allowed file types
91 *
92 * @var string
93 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +020094 public $allowed_types = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +020095
96 /**
97 * Temporary filename
98 *
99 * @var string
100 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200101 public $file_temp = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200102
103 /**
104 * Filename
105 *
106 * @var string
107 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200108 public $file_name = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200109
110 /**
111 * Original filename
112 *
113 * @var string
114 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200115 public $orig_name = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200116
117 /**
118 * File type
119 *
120 * @var string
121 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200122 public $file_type = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200123
124 /**
125 * File size
126 *
127 * @var int
128 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200129 public $file_size = NULL;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200130
131 /**
132 * Filename extension
133 *
134 * @var string
135 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200136 public $file_ext = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200137
138 /**
Adrianf496d132013-06-24 15:56:45 +0200139 * Force filename extension to lowercase
140 *
141 * @var string
142 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200143 public $file_ext_tolower = FALSE;
Adrianf496d132013-06-24 15:56:45 +0200144
145 /**
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200146 * Upload path
147 *
148 * @var string
149 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200150 public $upload_path = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200151
152 /**
153 * Overwrite flag
154 *
155 * @var bool
156 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200157 public $overwrite = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200158
159 /**
160 * Obfuscate filename flag
161 *
162 * @var bool
163 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200164 public $encrypt_name = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200165
166 /**
167 * Is image flag
168 *
169 * @var bool
170 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200171 public $is_image = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200172
173 /**
174 * Image width
175 *
176 * @var int
177 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200178 public $image_width = NULL;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200179
180 /**
181 * Image height
182 *
183 * @var int
184 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200185 public $image_height = NULL;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200186
187 /**
188 * Image type
189 *
190 * @var string
191 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200192 public $image_type = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200193
194 /**
195 * Image size string
196 *
197 * @var string
198 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200199 public $image_size_str = '';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200200
201 /**
202 * Error messages list
203 *
204 * @var array
205 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200206 public $error_msg = array();
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200207
208 /**
209 * Remove spaces flag
210 *
211 * @var bool
212 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200213 public $remove_spaces = TRUE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200214
215 /**
216 * MIME detection flag
217 *
218 * @var bool
219 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200220 public $detect_mime = TRUE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200221
222 /**
223 * XSS filter flag
224 *
225 * @var bool
226 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200227 public $xss_clean = FALSE;
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200228
229 /**
Andrey Andreev32c72122013-10-21 15:35:05 +0300230 * Apache mod_mime fix flag
231 *
232 * @var bool
233 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200234 public $mod_mime_fix = TRUE;
Andrey Andreev32c72122013-10-21 15:35:05 +0300235
236 /**
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200237 * Temporary filename prefix
238 *
239 * @var string
240 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200241 public $temp_prefix = 'temp_file_';
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200242
243 /**
244 * Filename sent by the client
245 *
246 * @var bool
247 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200248 public $client_name = '';
Barry Mienydd671972010-10-04 16:33:58 +0200249
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200250 // --------------------------------------------------------------------
251
252 /**
253 * Filename override
254 *
255 * @var string
256 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200257 protected $_file_name_override = '';
258
259 /**
260 * MIME types list
261 *
262 * @var array
263 */
264 protected $_mimes = array();
Barry Mienydd671972010-10-04 16:33:58 +0200265
Andrey Andreev119d8a72014-01-08 15:27:53 +0200266 /**
267 * CI Singleton
268 *
269 * @var object
270 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200271 protected $_CI;
Andrey Andreev119d8a72014-01-08 15:27:53 +0200272
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200273 // --------------------------------------------------------------------
274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 /**
276 * Constructor
277 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200278 * @param array $props
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300279 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200281 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200283 empty($config) OR $this->initialize($config, FALSE);
Barry Mienydd671972010-10-04 16:33:58 +0200284
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200285 $this->_mimes =& get_mimes();
286 $this->_CI =& get_instance();
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300287
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300288 log_message('debug', 'Upload Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 /**
294 * Initialize preferences
295 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200296 * @param array $config
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200297 * @param bool $reset
298 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200299 */
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200300 public function initialize(array $config = array(), $reset = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200302 $reflection = new ReflectionClass($this);
Barry Mienydd671972010-10-04 16:33:58 +0200303
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200304 if ($reset === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200306 $defaults = $reflection->getDefaultProperties();
307 foreach (array_keys($defaults) as $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200309 if ($key[0] === '_')
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200311 continue;
312 }
313
314 if (isset($config[$key]))
315 {
316 if ($reflection->hasMethod('set_'.$key))
317 {
318 $this->{'set_'.$key}($config[$key]);
319 }
320 else
321 {
322 $this->$key = $config[$key];
323 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 }
325 else
326 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200327 $this->$key = $defaults[$key];
Barry Mienydd671972010-10-04 16:33:58 +0200328 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200330
331 return $this;
332 }
333
334 foreach ($config as $key => &$value)
335 {
336 if ($key[0] !== '_' && $reflection->hasProperty($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200338 if ($reflection->hasMethod('set_'.$key))
339 {
340 $this->{'set_'.$key}($value);
341 }
342 else
343 {
344 $this->$key = $value;
345 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 }
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Jonese9d723f2010-07-12 10:10:59 -0500349 // if a file_name was provided in the config, use it instead of the user input
350 // supplied file name for all uploads until initialized again
351 $this->_file_name_override = $this->file_name;
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200352 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 /**
358 * Perform the file upload
359 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200360 * @param string $field
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200362 */
Greg Aker58fdee82010-11-10 15:07:09 -0600363 public function do_upload($field = 'userfile')
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300365 // Is $_FILES[$field] set? If not, no reason to continue.
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200366 if (isset($_FILES[$field]))
367 {
368 $_file = $_FILES[$field];
369 }
370 // Does the field name contain array notation?
371 elseif (($c = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $field, $matches)) > 1)
372 {
373 $_file = $_FILES;
374 for ($i = 0; $i < $c; $i++)
375 {
376 // We can't track numeric iterations, only full field names are accepted
377 if (($field = trim($matches[0][$i], '[]')) === '' OR ! isset($_file[$field]))
378 {
379 $_file = NULL;
380 break;
381 }
382
383 $_file = $_file[$field];
384 }
385 }
386
387 if ( ! isset($_file))
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
389 $this->set_error('upload_no_file_selected');
390 return FALSE;
391 }
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 // Is the upload path valid?
394 if ( ! $this->validate_upload_path())
395 {
396 // errors will already be set by validate_upload_path() so just return FALSE
397 return FALSE;
398 }
399
400 // Was the file able to be uploaded? If not, determine the reason why.
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200401 if ( ! is_uploaded_file($_file['tmp_name']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200403 $error = isset($_file['error']) ? $_file['error'] : 4;
Derek Allard2067d1a2008-11-13 22:59:24 +0000404
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300405 switch ($error)
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 {
Darren Benneye123a602013-03-30 18:17:54 +0000407 case UPLOAD_ERR_INI_SIZE:
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 $this->set_error('upload_file_exceeds_limit');
409 break;
Darren Benneye123a602013-03-30 18:17:54 +0000410 case UPLOAD_ERR_FORM_SIZE:
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 $this->set_error('upload_file_exceeds_form_limit');
412 break;
Darren Benneye123a602013-03-30 18:17:54 +0000413 case UPLOAD_ERR_PARTIAL:
Barry Mienydd671972010-10-04 16:33:58 +0200414 $this->set_error('upload_file_partial');
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 break;
Darren Benneye123a602013-03-30 18:17:54 +0000416 case UPLOAD_ERR_NO_FILE:
Barry Mienydd671972010-10-04 16:33:58 +0200417 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 break;
Darren Benneye123a602013-03-30 18:17:54 +0000419 case UPLOAD_ERR_NO_TMP_DIR:
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 $this->set_error('upload_no_temp_directory');
421 break;
Darren Benneye123a602013-03-30 18:17:54 +0000422 case UPLOAD_ERR_CANT_WRITE:
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 $this->set_error('upload_unable_to_write_file');
424 break;
Darren Benneye123a602013-03-30 18:17:54 +0000425 case UPLOAD_ERR_EXTENSION:
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 $this->set_error('upload_stopped_by_extension');
427 break;
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300428 default:
429 $this->set_error('upload_no_file_selected');
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 break;
431 }
432
433 return FALSE;
434 }
435
436 // Set the uploaded data as class variables
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200437 $this->file_temp = $_file['tmp_name'];
438 $this->file_size = $_file['size'];
Andrey Andreevd60e7002012-06-17 00:03:03 +0300439
440 // Skip MIME type detection?
441 if ($this->detect_mime !== FALSE)
442 {
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200443 $this->_file_mime_type($_file);
Andrey Andreevd60e7002012-06-17 00:03:03 +0300444 }
445
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300446 $this->file_type = preg_replace('/^(.+?);.*$/', '\\1', $this->file_type);
Derek Jones616fb022010-04-22 16:52:18 -0500447 $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
Andrey Andreeve7d017b2014-02-25 12:23:34 +0200448 $this->file_name = $this->_prep_filename($_file['name']);
Derek Jonese9d723f2010-07-12 10:10:59 -0500449 $this->file_ext = $this->get_extension($this->file_name);
450 $this->client_name = $this->file_name;
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 // Is the file type allowed to be uploaded?
453 if ( ! $this->is_allowed_filetype())
454 {
455 $this->set_error('upload_invalid_filetype');
456 return FALSE;
457 }
458
Derek Jonese9d723f2010-07-12 10:10:59 -0500459 // if we're overriding, let's now make sure the new name and type is allowed
Alex Bilbied261b1e2012-06-02 11:12:16 +0100460 if ($this->_file_name_override !== '')
Derek Jonese9d723f2010-07-12 10:10:59 -0500461 {
462 $this->file_name = $this->_prep_filename($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000463
464 // If no extension was provided in the file_name config item, use the uploaded one
Pascal Kriete14287f32011-02-14 13:39:34 -0500465 if (strpos($this->_file_name_override, '.') === FALSE)
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000466 {
467 $this->file_name .= $this->file_ext;
468 }
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000469 else
470 {
vlakoff35672462013-02-15 01:36:04 +0100471 // An extension was provided, let's have it!
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300472 $this->file_ext = $this->get_extension($this->_file_name_override);
Phil Sturgeon1e74da22010-12-15 10:45:06 +0000473 }
Derek Jonese9d723f2010-07-12 10:10:59 -0500474
475 if ( ! $this->is_allowed_filetype(TRUE))
476 {
477 $this->set_error('upload_invalid_filetype');
Barry Mienydd671972010-10-04 16:33:58 +0200478 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500479 }
480 }
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Jonese9d723f2010-07-12 10:10:59 -0500482 // Convert the file size to kilobytes
483 if ($this->file_size > 0)
484 {
485 $this->file_size = round($this->file_size/1024, 2);
486 }
487
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 // Is the file size within the allowed maximum?
489 if ( ! $this->is_allowed_filesize())
490 {
491 $this->set_error('upload_invalid_filesize');
492 return FALSE;
493 }
494
495 // Are the image dimensions within the allowed size?
vlakoffc941d852013-08-06 14:44:40 +0200496 // Note: This can fail if the server has an open_basedir restriction.
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 if ( ! $this->is_allowed_dimensions())
498 {
499 $this->set_error('upload_invalid_dimensions');
500 return FALSE;
501 }
502
503 // Sanitize the file name for security
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200504 $this->file_name = $this->_CI->security->sanitize_filename($this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 // Truncate the file name if it's too long
507 if ($this->max_filename > 0)
508 {
509 $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
510 }
511
512 // Remove white spaces in the name
Alex Bilbied261b1e2012-06-02 11:12:16 +0100513 if ($this->remove_spaces === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300515 $this->file_name = preg_replace('/\s+/', '_', $this->file_name);
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 }
517
518 /*
519 * Validate the file name
520 * This function appends an number onto the end of
521 * the file if one with the same name already exists.
522 * If it returns false there was a problem.
523 */
524 $this->orig_name = $this->file_name;
525
Alex Bilbied261b1e2012-06-02 11:12:16 +0100526 if ($this->overwrite === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 {
528 $this->file_name = $this->set_filename($this->upload_path, $this->file_name);
Barry Mienydd671972010-10-04 16:33:58 +0200529
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 if ($this->file_name === FALSE)
531 {
532 return FALSE;
533 }
534 }
535
536 /*
Derek Jonese9d723f2010-07-12 10:10:59 -0500537 * Run the file through the XSS hacking filter
538 * This helps prevent malicious code from being
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300539 * embedded within a file. Scripts can easily
Derek Jonese9d723f2010-07-12 10:10:59 -0500540 * be disguised as images or other file types.
541 */
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300542 if ($this->xss_clean && $this->do_xss_clean() === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500543 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300544 $this->set_error('upload_unable_to_write_file');
545 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500546 }
547
548 /*
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 * Move the file to the final destination
550 * To deal with different server configurations
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300551 * we'll attempt to use copy() first. If that fails
552 * we'll use move_uploaded_file(). One of the two should
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 * reliably work in most environments
554 */
555 if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
556 {
557 if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
558 {
Barry Mienydd671972010-10-04 16:33:58 +0200559 $this->set_error('upload_destination_error');
560 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 }
562 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000563
564 /*
565 * Set the finalized image dimensions
566 * This sets the image width/height (assuming the
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300567 * file was an image). We use this information
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 * in the "data" function.
569 */
570 $this->set_image_properties($this->upload_path.$this->file_name);
571
572 return TRUE;
573 }
Barry Mienydd671972010-10-04 16:33:58 +0200574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200576
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 /**
578 * Finalized Data Array
Barry Mienydd671972010-10-04 16:33:58 +0200579 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 * Returns an associative array containing all of the information
581 * related to the upload, allowing the developer easy access in one array.
582 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200583 * @param string $index
Michiel Vugteveen0ee287e2012-06-11 10:38:14 +0200584 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200585 */
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200586 public function data($index = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200588 $data = array(
Michiel Vugteveen3a7fb042012-06-11 09:29:16 +0200589 'file_name' => $this->file_name,
590 'file_type' => $this->file_type,
591 'file_path' => $this->upload_path,
592 'full_path' => $this->upload_path.$this->file_name,
593 'raw_name' => str_replace($this->file_ext, '', $this->file_name),
594 'orig_name' => $this->orig_name,
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300595 'client_name' => $this->client_name,
Michiel Vugteveen3a7fb042012-06-11 09:29:16 +0200596 'file_ext' => $this->file_ext,
597 'file_size' => $this->file_size,
598 'is_image' => $this->is_image(),
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300599 'image_width' => $this->image_width,
600 'image_height' => $this->image_height,
601 'image_type' => $this->image_type,
602 'image_size_str' => $this->image_size_str,
603 );
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200604
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200605 if ( ! empty($index))
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200606 {
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200607 return isset($data[$index]) ? $data[$index] : NULL;
Michiel Vugteveendca9f232012-06-11 09:17:14 +0200608 }
609
Michiel Vugteveen46a04292012-06-11 10:37:52 +0200610 return $data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 }
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200614
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 /**
616 * Set Upload Path
617 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200618 * @param string $path
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200619 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200620 */
Greg Aker58fdee82010-11-10 15:07:09 -0600621 public function set_upload_path($path)
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
623 // Make sure it has a trailing slash
624 $this->upload_path = rtrim($path, '/').'/';
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200625 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 }
Barry Mienydd671972010-10-04 16:33:58 +0200627
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200629
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 /**
631 * Set the file name
632 *
633 * This function takes a filename/path as input and looks for the
634 * existence of a file with the same name. If found, it will append a
635 * number to the end of the filename to avoid overwriting a pre-existing file.
636 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200637 * @param string $path
638 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200640 */
Greg Aker58fdee82010-11-10 15:07:09 -0600641 public function set_filename($path, $filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100643 if ($this->encrypt_name === TRUE)
Barry Mienydd671972010-10-04 16:33:58 +0200644 {
Barry Mienydd671972010-10-04 16:33:58 +0200645 $filename = md5(uniqid(mt_rand())).$this->file_ext;
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 }
Barry Mienydd671972010-10-04 16:33:58 +0200647
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 if ( ! file_exists($path.$filename))
649 {
650 return $filename;
651 }
Barry Mienydd671972010-10-04 16:33:58 +0200652
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 $filename = str_replace($this->file_ext, '', $filename);
Barry Mienydd671972010-10-04 16:33:58 +0200654
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 $new_filename = '';
Adam Jackettccbbea12011-08-21 16:19:11 -0400656 for ($i = 1; $i < $this->max_filename_increment; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200657 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 if ( ! file_exists($path.$filename.$i.$this->file_ext))
659 {
660 $new_filename = $filename.$i.$this->file_ext;
661 break;
662 }
663 }
664
Alex Bilbied261b1e2012-06-02 11:12:16 +0100665 if ($new_filename === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 {
667 $this->set_error('upload_bad_filename');
668 return FALSE;
669 }
670 else
671 {
672 return $new_filename;
673 }
674 }
Barry Mienydd671972010-10-04 16:33:58 +0200675
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200677
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 /**
679 * Set Maximum File Size
680 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200681 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200682 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200683 */
Greg Aker58fdee82010-11-10 15:07:09 -0600684 public function set_max_filesize($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000685 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200686 $this->max_size = ($n < 0) ? 0 : (int) $n;
687 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 }
Barry Mienydd671972010-10-04 16:33:58 +0200689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200691
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 /**
693 * Set Maximum File Name Length
694 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200695 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200696 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200697 */
Greg Aker58fdee82010-11-10 15:07:09 -0600698 public function set_max_filename($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200700 $this->max_filename = ($n < 0) ? 0 : (int) $n;
701 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 }
703
704 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 /**
707 * Set Maximum Image Width
708 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200709 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200710 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200711 */
Greg Aker58fdee82010-11-10 15:07:09 -0600712 public function set_max_width($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200714 $this->max_width = ($n < 0) ? 0 : (int) $n;
715 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 }
Barry Mienydd671972010-10-04 16:33:58 +0200717
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200719
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 /**
721 * Set Maximum Image Height
722 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200723 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200724 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200725 */
Greg Aker58fdee82010-11-10 15:07:09 -0600726 public function set_max_height($n)
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200728 $this->max_height = ($n < 0) ? 0 : (int) $n;
729 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 }
Barry Mienydd671972010-10-04 16:33:58 +0200731
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200733
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 /**
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200735 * Set minimum image width
736 *
737 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200738 * @return CI_Upload
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200739 */
740 public function set_min_width($n)
741 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200742 $this->min_width = ($n < 0) ? 0 : (int) $n;
743 return $this;
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200744 }
745
746 // --------------------------------------------------------------------
747
748 /**
749 * Set minimum image height
750 *
751 * @param int $n
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200752 * @return CI_Upload
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200753 */
754 public function set_min_height($n)
755 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200756 $this->min_height = ($n < 0) ? 0 : (int) $n;
757 return $this;
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200758 }
759
760 // --------------------------------------------------------------------
761
762 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 * Set Allowed File Types
764 *
Andrey Andreev82179bf2014-02-22 00:29:53 +0200765 * @param mixed $types
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200766 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200767 */
Greg Aker58fdee82010-11-10 15:07:09 -0600768 public function set_allowed_types($types)
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200770 $this->allowed_types = (is_array($types) OR $types === '*')
771 ? $types
772 : explode('|', $types);
773 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000774 }
Barry Mienydd671972010-10-04 16:33:58 +0200775
Derek Allard2067d1a2008-11-13 22:59:24 +0000776 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200777
Derek Allard2067d1a2008-11-13 22:59:24 +0000778 /**
779 * Set Image Properties
780 *
781 * Uses GD to determine the width/height/type of image
782 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200783 * @param string $path
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200784 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +0200785 */
Greg Aker58fdee82010-11-10 15:07:09 -0600786 public function set_image_properties($path = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200788 if ($this->is_image() && function_exists('getimagesize'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 {
790 if (FALSE !== ($D = @getimagesize($path)))
Barry Mienydd671972010-10-04 16:33:58 +0200791 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000792 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
793
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300794 $this->image_width = $D[0];
795 $this->image_height = $D[1];
796 $this->image_type = isset($types[$D[2]]) ? $types[$D[2]] : 'unknown';
797 $this->image_size_str = $D[3]; // string containing height and width
Derek Allard2067d1a2008-11-13 22:59:24 +0000798 }
799 }
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200800
801 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200805
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 /**
807 * Set XSS Clean
808 *
809 * Enables the XSS flag so that the file that was uploaded
810 * will be run through the XSS filter.
811 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200812 * @param bool $flag
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200813 * @return CI_Upload
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 */
Greg Aker58fdee82010-11-10 15:07:09 -0600815 public function set_xss_clean($flag = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100817 $this->xss_clean = ($flag === TRUE);
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200818 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 }
Barry Mienydd671972010-10-04 16:33:58 +0200820
Derek Allard2067d1a2008-11-13 22:59:24 +0000821 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200822
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 /**
824 * Validate the image
825 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200827 */
Greg Aker58fdee82010-11-10 15:07:09 -0600828 public function is_image()
Derek Allard2067d1a2008-11-13 22:59:24 +0000829 {
830 // IE will sometimes return odd mime-types during upload, so here we just standardize all
831 // jpegs or pngs to the same file type.
832
Derek Jones4b9c6292011-07-01 17:40:48 -0500833 $png_mimes = array('image/x-png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg');
Barry Mienydd671972010-10-04 16:33:58 +0200835
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 if (in_array($this->file_type, $png_mimes))
837 {
838 $this->file_type = 'image/png';
839 }
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300840 elseif (in_array($this->file_type, $jpeg_mimes))
Derek Allard2067d1a2008-11-13 22:59:24 +0000841 {
842 $this->file_type = 'image/jpeg';
843 }
844
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300845 $img_mimes = array('image/gif', 'image/jpeg', 'image/png');
Derek Allard2067d1a2008-11-13 22:59:24 +0000846
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300847 return in_array($this->file_type, $img_mimes, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 }
Barry Mienydd671972010-10-04 16:33:58 +0200849
Derek Allard2067d1a2008-11-13 22:59:24 +0000850 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200851
Derek Allard2067d1a2008-11-13 22:59:24 +0000852 /**
853 * Verify that the filetype is allowed
854 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200855 * @param bool $ignore_mime
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200857 */
Greg Aker58fdee82010-11-10 15:07:09 -0600858 public function is_allowed_filetype($ignore_mime = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000859 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200860 if ($this->allowed_types === '*')
Derek Jonese12f64e2010-03-02 22:55:08 -0600861 {
862 return TRUE;
863 }
Barry Mienydd671972010-10-04 16:33:58 +0200864
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200865 if (empty($this->allowed_types) OR ! is_array($this->allowed_types))
Derek Allard2067d1a2008-11-13 22:59:24 +0000866 {
867 $this->set_error('upload_no_file_types');
868 return FALSE;
869 }
Barry Mienydd671972010-10-04 16:33:58 +0200870
Derek Jonese9d723f2010-07-12 10:10:59 -0500871 $ext = strtolower(ltrim($this->file_ext, '.'));
Barry Mienydd671972010-10-04 16:33:58 +0200872
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200873 if ( ! in_array($ext, $this->allowed_types, TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000874 {
Derek Jonese9d723f2010-07-12 10:10:59 -0500875 return FALSE;
876 }
Derek Jonesafa282f2009-02-10 17:11:52 +0000877
Barry Mienydd671972010-10-04 16:33:58 +0200878 // Images get some additional checks
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200879 if (in_array($ext, array('gif', 'jpg', 'jpeg', 'jpe', 'png'), TRUE) && @getimagesize($this->file_temp) === FALSE)
Derek Jonese9d723f2010-07-12 10:10:59 -0500880 {
Andrey Andreev6ca407e2012-03-01 15:33:21 +0200881 return FALSE;
Derek Jonese9d723f2010-07-12 10:10:59 -0500882 }
Barry Mienydd671972010-10-04 16:33:58 +0200883
Derek Jonese9d723f2010-07-12 10:10:59 -0500884 if ($ignore_mime === TRUE)
885 {
886 return TRUE;
887 }
Barry Mienydd671972010-10-04 16:33:58 +0200888
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200889 if (isset($this->_mimes[$ext]))
Derek Jonese9d723f2010-07-12 10:10:59 -0500890 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +0200891 return is_array($this->_mimes[$ext])
892 ? in_array($this->file_type, $this->_mimes[$ext], TRUE)
893 : ($this->_mimes[$ext] === $this->file_type);
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 }
Barry Mienydd671972010-10-04 16:33:58 +0200895
Derek Allard2067d1a2008-11-13 22:59:24 +0000896 return FALSE;
897 }
Barry Mienydd671972010-10-04 16:33:58 +0200898
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200900
Derek Allard2067d1a2008-11-13 22:59:24 +0000901 /**
902 * Verify that the file is within the allowed size
903 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000904 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200905 */
Greg Aker58fdee82010-11-10 15:07:09 -0600906 public function is_allowed_filesize()
Derek Allard2067d1a2008-11-13 22:59:24 +0000907 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100908 return ($this->max_size === 0 OR $this->max_size > $this->file_size);
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 }
Barry Mienydd671972010-10-04 16:33:58 +0200910
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200912
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 /**
914 * Verify that the image is within the allowed width/height
915 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200917 */
Greg Aker58fdee82010-11-10 15:07:09 -0600918 public function is_allowed_dimensions()
Derek Allard2067d1a2008-11-13 22:59:24 +0000919 {
920 if ( ! $this->is_image())
921 {
922 return TRUE;
923 }
924
925 if (function_exists('getimagesize'))
926 {
927 $D = @getimagesize($this->file_temp);
928
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300929 if ($this->max_width > 0 && $D[0] > $this->max_width)
Derek Allard2067d1a2008-11-13 22:59:24 +0000930 {
931 return FALSE;
932 }
933
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300934 if ($this->max_height > 0 && $D[1] > $this->max_height)
Derek Allard2067d1a2008-11-13 22:59:24 +0000935 {
936 return FALSE;
937 }
Andrey Andreev05aa2d62012-12-03 16:06:55 +0200938
939 if ($this->min_width > 0 && $D[0] < $this->min_width)
940 {
941 return FALSE;
942 }
943
944 if ($this->min_height > 0 && $D[1] < $this->min_height)
945 {
946 return FALSE;
947 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 }
949
950 return TRUE;
951 }
Barry Mienydd671972010-10-04 16:33:58 +0200952
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200954
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 /**
956 * Validate Upload Path
957 *
958 * Verifies that it is a valid upload path with proper permissions.
959 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000960 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200961 */
Greg Aker58fdee82010-11-10 15:07:09 -0600962 public function validate_upload_path()
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100964 if ($this->upload_path === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 {
966 $this->set_error('upload_no_filepath');
967 return FALSE;
968 }
Barry Mienydd671972010-10-04 16:33:58 +0200969
Andrey Andreevc839d282012-06-07 14:35:27 +0300970 if (@realpath($this->upload_path) !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300972 $this->upload_path = str_replace('\\', '/', realpath($this->upload_path));
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 }
974
975 if ( ! @is_dir($this->upload_path))
976 {
977 $this->set_error('upload_no_filepath');
978 return FALSE;
979 }
980
981 if ( ! is_really_writable($this->upload_path))
982 {
983 $this->set_error('upload_not_writable');
984 return FALSE;
985 }
986
Andrey Andreev8e6f7a92012-03-26 20:13:00 +0300987 $this->upload_path = preg_replace('/(.+?)\/*$/', '\\1/', $this->upload_path);
Derek Allard2067d1a2008-11-13 22:59:24 +0000988 return TRUE;
989 }
Barry Mienydd671972010-10-04 16:33:58 +0200990
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200992
Derek Allard2067d1a2008-11-13 22:59:24 +0000993 /**
994 * Extract the file extension
995 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +0200996 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200998 */
Greg Aker58fdee82010-11-10 15:07:09 -0600999 public function get_extension($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 {
1001 $x = explode('.', $filename);
Adrianf496d132013-06-24 15:56:45 +02001002
Adrian74a228b2013-06-25 12:09:22 +02001003 if (count($x) === 1)
1004 {
1005 return '';
1006 }
1007
1008 $ext = ($this->file_ext_tolower) ? strtolower(end($x)) : end($x);
1009 return '.'.$ext;
Barry Mienydd671972010-10-04 16:33:58 +02001010 }
1011
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001013
Derek Allard2067d1a2008-11-13 22:59:24 +00001014 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 * Limit the File Name Length
1016 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +03001017 * @param string $filename
1018 * @param int $length
Derek Allard2067d1a2008-11-13 22:59:24 +00001019 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001020 */
Greg Aker58fdee82010-11-10 15:07:09 -06001021 public function limit_filename_length($filename, $length)
Derek Allard2067d1a2008-11-13 22:59:24 +00001022 {
1023 if (strlen($filename) < $length)
1024 {
1025 return $filename;
1026 }
Barry Mienydd671972010-10-04 16:33:58 +02001027
Derek Allard2067d1a2008-11-13 22:59:24 +00001028 $ext = '';
1029 if (strpos($filename, '.') !== FALSE)
1030 {
1031 $parts = explode('.', $filename);
1032 $ext = '.'.array_pop($parts);
1033 $filename = implode('.', $parts);
1034 }
Barry Mienydd671972010-10-04 16:33:58 +02001035
Derek Allard2067d1a2008-11-13 22:59:24 +00001036 return substr($filename, 0, ($length - strlen($ext))).$ext;
1037 }
1038
1039 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001040
Derek Allard2067d1a2008-11-13 22:59:24 +00001041 /**
1042 * Runs the file through the XSS clean function
1043 *
1044 * This prevents people from embedding malicious code in their files.
1045 * I'm not sure that it won't negatively affect certain files in unexpected ways,
1046 * but so far I haven't found that it causes trouble.
1047 *
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001048 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001049 */
Greg Aker58fdee82010-11-10 15:07:09 -06001050 public function do_xss_clean()
Barry Mienydd671972010-10-04 16:33:58 +02001051 {
Derek Jonese9d723f2010-07-12 10:10:59 -05001052 $file = $this->file_temp;
Barry Mienydd671972010-10-04 16:33:58 +02001053
Andrey Andreev5036c9c2012-06-04 15:34:56 +03001054 if (filesize($file) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 {
1056 return FALSE;
1057 }
Barry Mienydd671972010-10-04 16:33:58 +02001058
Andrey Andreevc839d282012-06-07 14:35:27 +03001059 if (memory_get_usage() && ($memory_limit = ini_get('memory_limit')))
Greg Akerf82e51c2010-04-14 19:33:50 -05001060 {
Andrey Andreevc839d282012-06-07 14:35:27 +03001061 $memory_limit *= 1024 * 1024;
Barry Mienydd671972010-10-04 16:33:58 +02001062
Greg Akerc78a2592010-06-09 11:45:32 -05001063 // There was a bug/behavioural change in PHP 5.2, where numbers over one million get output
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001064 // into scientific notation. number_format() ensures this number is an integer
Greg Akerc78a2592010-06-09 11:45:32 -05001065 // http://bugs.php.net/bug.php?id=43053
Barry Mienydd671972010-10-04 16:33:58 +02001066
Andrey Andreevc839d282012-06-07 14:35:27 +03001067 $memory_limit = number_format(ceil(filesize($file) + $memory_limit), 0, '.', '');
Barry Mienydd671972010-10-04 16:33:58 +02001068
Andrey Andreevc839d282012-06-07 14:35:27 +03001069 ini_set('memory_limit', $memory_limit); // When an integer is used, the value is measured in bytes. - PHP.net
Greg Akerf82e51c2010-04-14 19:33:50 -05001070 }
1071
1072 // If the file being uploaded is an image, then we should have no problem with XSS attacks (in theory), but
1073 // IE can be fooled into mime-type detecting a malformed image as an html file, thus executing an XSS attack on anyone
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001074 // using IE who looks at the image. It does this by inspecting the first 255 bytes of an image. To get around this
1075 // CI will itself look at the first 255 bytes of an image to determine its relative safety. This can save a lot of
Barry Mienydd671972010-10-04 16:33:58 +02001076 // processor power and time if it is actually a clean image, as it will be in nearly all instances _except_ an
Greg Akerf82e51c2010-04-14 19:33:50 -05001077 // attempted XSS attack.
1078
1079 if (function_exists('getimagesize') && @getimagesize($file) !== FALSE)
1080 {
Barry Mienydd671972010-10-04 16:33:58 +02001081 if (($file = @fopen($file, 'rb')) === FALSE) // "b" to force binary
1082 {
Greg Akerf82e51c2010-04-14 19:33:50 -05001083 return FALSE; // Couldn't open the file, return FALSE
Barry Mienydd671972010-10-04 16:33:58 +02001084 }
Greg Akerf82e51c2010-04-14 19:33:50 -05001085
Barry Mienydd671972010-10-04 16:33:58 +02001086 $opening_bytes = fread($file, 256);
1087 fclose($file);
Greg Akerf82e51c2010-04-14 19:33:50 -05001088
1089 // These are known to throw IE into mime-type detection chaos
1090 // <a, <body, <head, <html, <img, <plaintext, <pre, <script, <table, <title
1091 // title is basically just in SVG, but we filter it anyhow
1092
vlakoff35672462013-02-15 01:36:04 +01001093 // if it's an image or no "triggers" detected in the first 256 bytes - we're good
Andrey Andreevc839d282012-06-07 14:35:27 +03001094 return ! preg_match('/<(a|body|head|html|img|plaintext|pre|script|table|title)[\s>]/i', $opening_bytes);
Greg Akerf82e51c2010-04-14 19:33:50 -05001095 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001096
1097 if (($data = @file_get_contents($file)) === FALSE)
1098 {
1099 return FALSE;
1100 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001101
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001102 return $this->_CI->security->xss_clean($data, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 }
Barry Mienydd671972010-10-04 16:33:58 +02001104
Derek Allard2067d1a2008-11-13 22:59:24 +00001105 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001106
Derek Allard2067d1a2008-11-13 22:59:24 +00001107 /**
1108 * Set an error message
1109 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001110 * @param string $msg
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001111 * @return CI_Upload
Barry Mienydd671972010-10-04 16:33:58 +02001112 */
Greg Aker58fdee82010-11-10 15:07:09 -06001113 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +00001114 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001115 $this->_CI->lang->load('upload');
Barry Mienydd671972010-10-04 16:33:58 +02001116
Andrey Andreev119d8a72014-01-08 15:27:53 +02001117 is_array($msg) OR $msg = array($msg);
Darren Benney4a8d1902013-03-30 20:07:49 +00001118
Darren Benney38d5f4b2013-03-30 21:00:38 +00001119 foreach ($msg as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001120 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001121 $msg = ($this->_CI->lang->line($val) === FALSE) ? $val : $this->_CI->lang->line($val);
Derek Allard2067d1a2008-11-13 22:59:24 +00001122 $this->error_msg[] = $msg;
1123 log_message('error', $msg);
1124 }
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001125
1126 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +00001127 }
Barry Mienydd671972010-10-04 16:33:58 +02001128
Derek Allard2067d1a2008-11-13 22:59:24 +00001129 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001130
Derek Allard2067d1a2008-11-13 22:59:24 +00001131 /**
1132 * Display the error message
1133 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001134 * @param string $open
1135 * @param string $close
Derek Allard2067d1a2008-11-13 22:59:24 +00001136 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001137 */
Greg Aker58fdee82010-11-10 15:07:09 -06001138 public function display_errors($open = '<p>', $close = '</p>')
Derek Allard2067d1a2008-11-13 22:59:24 +00001139 {
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001140 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 }
Barry Mienydd671972010-10-04 16:33:58 +02001142
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001144
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 /**
Derek Allard2067d1a2008-11-13 22:59:24 +00001146 * Prep Filename
1147 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001148 * Prevents possible script execution from Apache's handling
1149 * of files' multiple extensions.
Derek Allard2067d1a2008-11-13 22:59:24 +00001150 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001151 * @link http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext
1152 *
1153 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +00001154 * @return string
1155 */
Greg Aker58fdee82010-11-10 15:07:09 -06001156 protected function _prep_filename($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +00001157 {
Andrey Andreev32c72122013-10-21 15:35:05 +03001158 if ($this->mod_mime_fix === FALSE OR $this->allowed_types === '*' OR strpos($filename, '.') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001159 {
1160 return $filename;
1161 }
Derek Allard616dab82009-02-16 15:44:32 +00001162
Derek Allard2067d1a2008-11-13 22:59:24 +00001163 $parts = explode('.', $filename);
1164 $ext = array_pop($parts);
1165 $filename = array_shift($parts);
Derek Allard616dab82009-02-16 15:44:32 +00001166
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 foreach ($parts as $part)
1168 {
Andrey Andreev2cf4c9b2014-02-21 15:01:48 +02001169 if ( ! in_array(strtolower($part), $this->allowed_types) OR ! isset($this->_mimes[strtolower($part)]))
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 {
1171 $filename .= '.'.$part.'_';
1172 }
1173 else
1174 {
1175 $filename .= '.'.$part;
1176 }
1177 }
Derek Allardd70b0642009-02-16 13:51:42 +00001178
Andrey Andreev8e6f7a92012-03-26 20:13:00 +03001179 return $filename.'.'.$ext;
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 }
1181
1182 // --------------------------------------------------------------------
1183
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001184 /**
1185 * File MIME type
1186 *
1187 * Detects the (actual) MIME type of the uploaded file, if possible.
1188 * The input array is expected to be $_FILES[$field]
1189 *
Andrey Andreevf5ccd122012-11-02 00:17:39 +02001190 * @param array $file
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001191 * @return void
1192 */
1193 protected function _file_mime_type($file)
1194 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001195 // We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
Andrey Andreevf7aed122011-12-13 11:01:06 +02001196 $regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001197
1198 /* Fileinfo extension - most reliable method
1199 *
1200 * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the
1201 * more convenient FILEINFO_MIME_TYPE flag doesn't exist.
1202 */
1203 if (function_exists('finfo_file'))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001204 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001205 $finfo = @finfo_open(FILEINFO_MIME);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001206 if (is_resource($finfo)) // It is possible that a FALSE value is returned, if there is no magic MIME database file found on the system
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001207 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001208 $mime = @finfo_file($finfo, $file['tmp_name']);
1209 finfo_close($finfo);
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001210
1211 /* According to the comments section of the PHP manual page,
1212 * it is possible that this function returns an empty string
Andrey Andreev2cec39f2011-09-24 22:59:37 +03001213 * for some files (e.g. if they don't exist in the magic MIME database)
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001214 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001215 if (is_string($mime) && preg_match($regexp, $mime, $matches))
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001216 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001217 $this->file_type = $matches[1];
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001218 return;
1219 }
1220 }
1221 }
1222
Andrey Andreeva49e3812011-12-09 13:05:22 +02001223 /* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type,
1224 * which is still more secure than depending on the value of $_FILES[$field]['type'], and as it
1225 * was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better
1226 * than mime_content_type() as well, hence the attempts to try calling the command line with
1227 * three different functions.
Andrey Andreev6700b932011-09-24 14:25:33 +03001228 *
1229 * Notes:
Andrey Andreev59654312011-12-02 14:28:54 +02001230 * - the DIRECTORY_SEPARATOR comparison ensures that we're not on a Windows system
Andrey Andreeva49e3812011-12-09 13:05:22 +02001231 * - many system admins would disable the exec(), shell_exec(), popen() and similar functions
vlakofff3994252013-02-19 01:45:23 +01001232 * due to security concerns, hence the function_usable() checks
Andrey Andreev6700b932011-09-24 14:25:33 +03001233 */
Andrey Andreeva49e3812011-12-09 13:05:22 +02001234 if (DIRECTORY_SEPARATOR !== '\\')
Andrey Andreev6700b932011-09-24 14:25:33 +03001235 {
Andrey Andreevd60e7002012-06-17 00:03:03 +03001236 $cmd = function_exists('escapeshellarg')
1237 ? 'file --brief --mime '.escapeshellarg($file['tmp_name']).' 2>&1'
1238 : 'file --brief --mime '.$file['tmp_name'].' 2>&1';
Andrey Andreeva49e3812011-12-09 13:05:22 +02001239
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001240 if (function_usable('exec'))
Andrey Andreev6700b932011-09-24 14:25:33 +03001241 {
Andrey Andreeva49e3812011-12-09 13:05:22 +02001242 /* This might look confusing, as $mime is being populated with all of the output when set in the second parameter.
vlakofff3994252013-02-19 01:45:23 +01001243 * However, we only need the last line, which is the actual return value of exec(), and as such - it overwrites
Andrey Andreeva49e3812011-12-09 13:05:22 +02001244 * anything that could already be set for $mime previously. This effectively makes the second parameter a dummy
1245 * value, which is only put to allow us to get the return status code.
1246 */
1247 $mime = @exec($cmd, $mime, $return_status);
1248 if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches))
1249 {
1250 $this->file_type = $matches[1];
1251 return;
1252 }
1253 }
1254
Andrey Andreevf6274742014-02-20 18:05:58 +02001255 if ( ! ini_get('safe_mode') && function_usable('shell_exec'))
Andrey Andreeva49e3812011-12-09 13:05:22 +02001256 {
1257 $mime = @shell_exec($cmd);
1258 if (strlen($mime) > 0)
1259 {
1260 $mime = explode("\n", trim($mime));
1261 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1262 {
1263 $this->file_type = $matches[1];
1264 return;
1265 }
1266 }
1267 }
1268
Andrey Andreeve9d2dc82012-11-07 14:23:29 +02001269 if (function_usable('popen'))
Andrey Andreeva49e3812011-12-09 13:05:22 +02001270 {
1271 $proc = @popen($cmd, 'r');
1272 if (is_resource($proc))
1273 {
tubalmartin010f1f42012-03-03 22:24:31 +01001274 $mime = @fread($proc, 512);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001275 @pclose($proc);
1276 if ($mime !== FALSE)
1277 {
1278 $mime = explode("\n", trim($mime));
1279 if (preg_match($regexp, $mime[(count($mime) - 1)], $matches))
1280 {
1281 $this->file_type = $matches[1];
1282 return;
1283 }
1284 }
1285 }
1286 }
1287 }
1288
1289 // Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field]['type'])
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001290 if (function_exists('mime_content_type'))
1291 {
1292 $this->file_type = @mime_content_type($file['tmp_name']);
Andrey Andreeva49e3812011-12-09 13:05:22 +02001293 if (strlen($this->file_type) > 0) // It's possible that mime_content_type() returns FALSE or an empty string
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001294 {
Andrey Andreev3a3c9472011-09-24 14:25:33 +03001295 return;
1296 }
1297 }
1298
1299 $this->file_type = $file['type'];
1300 }
1301
Derek Allard2067d1a2008-11-13 22:59:24 +00001302}
Derek Allard2067d1a2008-11-13 22:59:24 +00001303
1304/* End of file Upload.php */
Adrian74a228b2013-06-25 12:09:22 +02001305/* Location: ./system/libraries/Upload.php */