Public Member Functions | |
| CI_Upload ($props=array()) | |
| Constructor. | |
| initialize ($config=array()) | |
| Initialize preferences. | |
| do_upload ($field= 'userfile') | |
| Perform the file upload. | |
| data () | |
| Finalized Data Array. | |
| set_upload_path ($path) | |
| Set Upload Path. | |
| set_filename ($path, $filename) | |
| Set the file name. | |
| set_max_filesize ($n) | |
| Set Maximum File Size. | |
| set_max_width ($n) | |
| Set Maximum Image Width. | |
| set_max_height ($n) | |
| Set Maximum Image Height. | |
| set_allowed_types ($types) | |
| Set Allowed File Types. | |
| set_image_properties ($path= '') | |
| Set Image Properties. | |
| set_xss_clean ($flag=FALSE) | |
| Set XSS Clean. | |
| is_image () | |
| Validate the image. | |
| is_allowed_filetype () | |
| Verify that the filetype is allowed. | |
| is_allowed_filesize () | |
| Verify that the file is within the allowed size. | |
| is_allowed_dimensions () | |
| Verify that the image is within the allowed width/height. | |
| validate_upload_path () | |
| Validate Upload Path. | |
| get_extension ($filename) | |
| Extract the file extension. | |
| clean_file_name ($filename) | |
| Clean the file name for security. | |
| do_xss_clean () | |
| Runs the file through the XSS clean function. | |
| set_error ($msg) | |
| Set an error message. | |
| display_errors ($open= '< p >', $close= '</p >') | |
| Display the error message. | |
| mimes_types ($mime) | |
| List of Mime Types. | |
| _prep_filename ($filename) | |
| Prep Filename. | |
Public Attributes | |
| $max_size = 0 | |
| $max_width = 0 | |
| $max_height = 0 | |
| $allowed_types = "" | |
| $file_temp = "" | |
| $file_name = "" | |
| $orig_name = "" | |
| $file_type = "" | |
| $file_size = "" | |
| $file_ext = "" | |
| $upload_path = "" | |
| $overwrite = FALSE | |
| $encrypt_name = FALSE | |
| $is_image = FALSE | |
| $image_width = '' | |
| $image_height = '' | |
| $image_type = '' | |
| $image_size_str = '' | |
| $error_msg = array() | |
| $mimes = array() | |
| $remove_spaces = TRUE | |
| $xss_clean = FALSE | |
| $temp_prefix = "temp_file_" | |
Definition at line 27 of file Upload.php.
| CI_Upload::_prep_filename | ( | $ | filename | ) |
Prep Filename.
Prevents possible script execution from Apache's handling of files multiple extensions http://httpd.apache.org/docs/1.3/mod/mod_mime.html#multipleext
private
| string |
Definition at line 848 of file Upload.php.
References mimes_types().
Referenced by do_upload().
00849 { 00850 if (strpos($filename, '.') === FALSE) 00851 { 00852 return $filename; 00853 } 00854 00855 $parts = explode('.', $filename); 00856 $ext = array_pop($parts); 00857 $filename = array_shift($parts); 00858 00859 foreach ($parts as $part) 00860 { 00861 if ($this->mimes_types(strtolower($part)) === FALSE) 00862 { 00863 $filename .= '.'.$part.'_'; 00864 } 00865 else 00866 { 00867 $filename .= '.'.$part; 00868 } 00869 } 00870 00871 $filename .= '.'.$ext; 00872 00873 return $filename; 00874 }


| CI_Upload::CI_Upload | ( | $ | props = array() |
) |
Constructor.
public
Definition at line 58 of file Upload.php.
References initialize(), and log_message().
00059 { 00060 if (count($props) > 0) 00061 { 00062 $this->initialize($props); 00063 } 00064 00065 log_message('debug', "Upload Class Initialized"); 00066 }

| CI_Upload::clean_file_name | ( | $ | filename | ) |
Clean the file name for security.
public
| string |
Definition at line 679 of file Upload.php.
Referenced by do_upload().
00680 { 00681 $bad = array( 00682 "<!--", 00683 "-->", 00684 "'", 00685 "<", 00686 ">", 00687 '"', 00688 '&', 00689 '$', 00690 '=', 00691 ';', 00692 '?', 00693 '/', 00694 "%20", 00695 "%22", 00696 "%3c", // < 00697 "%253c", // < 00698 "%3e", // > 00699 "%0e", // > 00700 "%28", // ( 00701 "%29", // ) 00702 "%2528", // ( 00703 "%26", // & 00704 "%24", // $ 00705 "%3f", // ? 00706 "%3b", // ; 00707 "%3d" // = 00708 ); 00709 00710 foreach ($bad as $val) 00711 { 00712 $filename = str_replace($val, '', $filename); 00713 } 00714 00715 return stripslashes($filename); 00716 }

| CI_Upload::data | ( | ) |
Finalized Data Array.
Returns an associative array containing all of the information related to the upload, allowing the developer easy access in one array.
public
Definition at line 298 of file Upload.php.
References is_image().
00299 { 00300 return array ( 00301 'file_name' => $this->file_name, 00302 'file_type' => $this->file_type, 00303 'file_path' => $this->upload_path, 00304 'full_path' => $this->upload_path.$this->file_name, 00305 'raw_name' => str_replace($this->file_ext, '', $this->file_name), 00306 'orig_name' => $this->orig_name, 00307 'file_ext' => $this->file_ext, 00308 'file_size' => $this->file_size, 00309 'is_image' => $this->is_image(), 00310 'image_width' => $this->image_width, 00311 'image_height' => $this->image_height, 00312 'image_type' => $this->image_type, 00313 'image_size_str' => $this->image_size_str, 00314 ); 00315 }

| CI_Upload::display_errors | ( | $ | open = '<p>', |
|
| $ | close = '</p>' | |||
| ) |
Display the error message.
public
| string | ||
| string |
Definition at line 799 of file Upload.php.
00800 { 00801 $str = ''; 00802 foreach ($this->error_msg as $val) 00803 { 00804 $str .= $open.$val.$close; 00805 } 00806 00807 return $str; 00808 }
| CI_Upload::do_upload | ( | $ | field = 'userfile' |
) |
Perform the file upload.
public
Definition at line 135 of file Upload.php.
References _prep_filename(), clean_file_name(), do_xss_clean(), get_extension(), is_allowed_dimensions(), is_allowed_filesize(), is_allowed_filetype(), set_error(), set_filename(), set_image_properties(), and validate_upload_path().
00136 { 00137 // Is $_FILES[$field] set? If not, no reason to continue. 00138 if ( ! isset($_FILES[$field])) 00139 { 00140 $this->set_error('upload_no_file_selected'); 00141 return FALSE; 00142 } 00143 00144 // Is the upload path valid? 00145 if ( ! $this->validate_upload_path()) 00146 { 00147 // errors will already be set by validate_upload_path() so just return FALSE 00148 return FALSE; 00149 } 00150 00151 // Was the file able to be uploaded? If not, determine the reason why. 00152 if ( ! is_uploaded_file($_FILES[$field]['tmp_name'])) 00153 { 00154 $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error']; 00155 00156 switch($error) 00157 { 00158 case 1: // UPLOAD_ERR_INI_SIZE 00159 $this->set_error('upload_file_exceeds_limit'); 00160 break; 00161 case 2: // UPLOAD_ERR_FORM_SIZE 00162 $this->set_error('upload_file_exceeds_form_limit'); 00163 break; 00164 case 3: // UPLOAD_ERR_PARTIAL 00165 $this->set_error('upload_file_partial'); 00166 break; 00167 case 4: // UPLOAD_ERR_NO_FILE 00168 $this->set_error('upload_no_file_selected'); 00169 break; 00170 case 6: // UPLOAD_ERR_NO_TMP_DIR 00171 $this->set_error('upload_no_temp_directory'); 00172 break; 00173 case 7: // UPLOAD_ERR_CANT_WRITE 00174 $this->set_error('upload_unable_to_write_file'); 00175 break; 00176 case 8: // UPLOAD_ERR_EXTENSION 00177 $this->set_error('upload_stopped_by_extension'); 00178 break; 00179 default : $this->set_error('upload_no_file_selected'); 00180 break; 00181 } 00182 00183 return FALSE; 00184 } 00185 00186 // Set the uploaded data as class variables 00187 $this->file_temp = $_FILES[$field]['tmp_name']; 00188 $this->file_name = $this->_prep_filename($_FILES[$field]['name']); 00189 $this->file_size = $_FILES[$field]['size']; 00190 $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']); 00191 $this->file_type = strtolower($this->file_type); 00192 $this->file_ext = $this->get_extension($_FILES[$field]['name']); 00193 00194 // Convert the file size to kilobytes 00195 if ($this->file_size > 0) 00196 { 00197 $this->file_size = round($this->file_size/1024, 2); 00198 } 00199 00200 // Is the file type allowed to be uploaded? 00201 if ( ! $this->is_allowed_filetype()) 00202 { 00203 $this->set_error('upload_invalid_filetype'); 00204 return FALSE; 00205 } 00206 00207 // Is the file size within the allowed maximum? 00208 if ( ! $this->is_allowed_filesize()) 00209 { 00210 $this->set_error('upload_invalid_filesize'); 00211 return FALSE; 00212 } 00213 00214 // Are the image dimensions within the allowed size? 00215 // Note: This can fail if the server has an open_basdir restriction. 00216 if ( ! $this->is_allowed_dimensions()) 00217 { 00218 $this->set_error('upload_invalid_dimensions'); 00219 return FALSE; 00220 } 00221 00222 // Sanitize the file name for security 00223 $this->file_name = $this->clean_file_name($this->file_name); 00224 00225 // Remove white spaces in the name 00226 if ($this->remove_spaces == TRUE) 00227 { 00228 $this->file_name = preg_replace("/\s+/", "_", $this->file_name); 00229 } 00230 00231 /* 00232 * Validate the file name 00233 * This function appends an number onto the end of 00234 * the file if one with the same name already exists. 00235 * If it returns false there was a problem. 00236 */ 00237 $this->orig_name = $this->file_name; 00238 00239 if ($this->overwrite == FALSE) 00240 { 00241 $this->file_name = $this->set_filename($this->upload_path, $this->file_name); 00242 00243 if ($this->file_name === FALSE) 00244 { 00245 return FALSE; 00246 } 00247 } 00248 00249 /* 00250 * Move the file to the final destination 00251 * To deal with different server configurations 00252 * we'll attempt to use copy() first. If that fails 00253 * we'll use move_uploaded_file(). One of the two should 00254 * reliably work in most environments 00255 */ 00256 if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name)) 00257 { 00258 if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name)) 00259 { 00260 $this->set_error('upload_destination_error'); 00261 return FALSE; 00262 } 00263 } 00264 00265 /* 00266 * Run the file through the XSS hacking filter 00267 * This helps prevent malicious code from being 00268 * embedded within a file. Scripts can easily 00269 * be disguised as images or other file types. 00270 */ 00271 if ($this->xss_clean == TRUE) 00272 { 00273 $this->do_xss_clean(); 00274 } 00275 00276 /* 00277 * Set the finalized image dimensions 00278 * This sets the image width/height (assuming the 00279 * file was an image). We use this information 00280 * in the "data" function. 00281 */ 00282 $this->set_image_properties($this->upload_path.$this->file_name); 00283 00284 return TRUE; 00285 }

| CI_Upload::do_xss_clean | ( | ) |
Runs the file through the XSS clean function.
This prevents people from embedding malicious code in their files. I'm not sure that it won't negatively affect certain files in unexpected ways, but so far I haven't found that it causes trouble.
public
Definition at line 730 of file Upload.php.
References $CI, and get_instance().
Referenced by do_upload().
00731 { 00732 $file = $this->upload_path.$this->file_name; 00733 00734 if (filesize($file) == 0) 00735 { 00736 return FALSE; 00737 } 00738 00739 if (($data = @file_get_contents($file)) === FALSE) 00740 { 00741 return FALSE; 00742 } 00743 00744 if ( ! $fp = @fopen($file, FOPEN_READ_WRITE)) 00745 { 00746 return FALSE; 00747 } 00748 00749 $CI =& get_instance(); 00750 $data = $CI->input->xss_clean($data); 00751 00752 flock($fp, LOCK_EX); 00753 fwrite($fp, $data); 00754 flock($fp, LOCK_UN); 00755 fclose($fp); 00756 }


| CI_Upload::get_extension | ( | $ | filename | ) |
Extract the file extension.
public
| string |
Definition at line 664 of file Upload.php.
Referenced by do_upload().

| CI_Upload::initialize | ( | $ | config = array() |
) |
Initialize preferences.
public
| array |
Definition at line 77 of file Upload.php.
References $config, and $method.
Referenced by CI_Upload().
00078 { 00079 $defaults = array( 00080 'max_size' => 0, 00081 'max_width' => 0, 00082 'max_height' => 0, 00083 'allowed_types' => "", 00084 'file_temp' => "", 00085 'file_name' => "", 00086 'orig_name' => "", 00087 'file_type' => "", 00088 'file_size' => "", 00089 'file_ext' => "", 00090 'upload_path' => "", 00091 'overwrite' => FALSE, 00092 'encrypt_name' => FALSE, 00093 'is_image' => FALSE, 00094 'image_width' => '', 00095 'image_height' => '', 00096 'image_type' => '', 00097 'image_size_str' => '', 00098 'error_msg' => array(), 00099 'mimes' => array(), 00100 'remove_spaces' => TRUE, 00101 'xss_clean' => FALSE, 00102 'temp_prefix' => "temp_file_" 00103 ); 00104 00105 00106 foreach ($defaults as $key => $val) 00107 { 00108 if (isset($config[$key])) 00109 { 00110 $method = 'set_'.$key; 00111 if (method_exists($this, $method)) 00112 { 00113 $this->$method($config[$key]); 00114 } 00115 else 00116 { 00117 $this->$key = $config[$key]; 00118 } 00119 } 00120 else 00121 { 00122 $this->$key = $val; 00123 } 00124 } 00125 }

| CI_Upload::is_allowed_dimensions | ( | ) |
Verify that the image is within the allowed width/height.
public
Definition at line 588 of file Upload.php.
References is_image().
Referenced by do_upload().
00589 { 00590 if ( ! $this->is_image()) 00591 { 00592 return TRUE; 00593 } 00594 00595 if (function_exists('getimagesize')) 00596 { 00597 $D = @getimagesize($this->file_temp); 00598 00599 if ($this->max_width > 0 AND $D['0'] > $this->max_width) 00600 { 00601 return FALSE; 00602 } 00603 00604 if ($this->max_height > 0 AND $D['1'] > $this->max_height) 00605 { 00606 return FALSE; 00607 } 00608 00609 return TRUE; 00610 } 00611 00612 return TRUE; 00613 }


| CI_Upload::is_allowed_filesize | ( | ) |
Verify that the file is within the allowed size.
public
Definition at line 568 of file Upload.php.
Referenced by do_upload().
00569 { 00570 if ($this->max_size != 0 AND $this->file_size > $this->max_size) 00571 { 00572 return FALSE; 00573 } 00574 else 00575 { 00576 return TRUE; 00577 } 00578 }

| CI_Upload::is_allowed_filetype | ( | ) |
Verify that the filetype is allowed.
public
Definition at line 529 of file Upload.php.
References mimes_types(), and set_error().
Referenced by do_upload().
00530 { 00531 if (count($this->allowed_types) == 0 OR ! is_array($this->allowed_types)) 00532 { 00533 $this->set_error('upload_no_file_types'); 00534 return FALSE; 00535 } 00536 00537 foreach ($this->allowed_types as $val) 00538 { 00539 $mime = $this->mimes_types(strtolower($val)); 00540 00541 if (is_array($mime)) 00542 { 00543 if (in_array($this->file_type, $mime, TRUE)) 00544 { 00545 return TRUE; 00546 } 00547 } 00548 else 00549 { 00550 if ($mime == $this->file_type) 00551 { 00552 return TRUE; 00553 } 00554 } 00555 } 00556 00557 return FALSE; 00558 }


| CI_Upload::is_image | ( | ) |
Validate the image.
public
Definition at line 494 of file Upload.php.
Referenced by data(), is_allowed_dimensions(), and set_image_properties().
00495 { 00496 // IE will sometimes return odd mime-types during upload, so here we just standardize all 00497 // jpegs or pngs to the same file type. 00498 00499 $png_mimes = array('image/x-png'); 00500 $jpeg_mimes = array('image/jpg', 'image/jpe', 'image/jpeg', 'image/pjpeg'); 00501 00502 if (in_array($this->file_type, $png_mimes)) 00503 { 00504 $this->file_type = 'image/png'; 00505 } 00506 00507 if (in_array($this->file_type, $jpeg_mimes)) 00508 { 00509 $this->file_type = 'image/jpeg'; 00510 } 00511 00512 $img_mimes = array( 00513 'image/gif', 00514 'image/jpeg', 00515 'image/png', 00516 ); 00517 00518 return (in_array($this->file_type, $img_mimes, TRUE)) ? TRUE : FALSE; 00519 }

| CI_Upload::mimes_types | ( | $ | mime | ) |
List of Mime Types.
This is a list of mime types. We use it to validate the "allowed types" set by the developer
public
| string |
Definition at line 822 of file Upload.php.
References $mimes.
Referenced by _prep_filename(), and is_allowed_filetype().
00823 { 00824 global $mimes; 00825 00826 if (count($this->mimes) == 0) 00827 { 00828 if (@require_once(APPPATH.'config/mimes'.EXT)) 00829 { 00830 $this->mimes = $mimes; 00831 unset($mimes); 00832 } 00833 } 00834 00835 return ( ! isset($this->mimes[$mime])) ? FALSE : $this->mimes[$mime]; 00836 }

| CI_Upload::set_allowed_types | ( | $ | types | ) |
Set Allowed File Types.
public
| string |
Definition at line 432 of file Upload.php.
| CI_Upload::set_error | ( | $ | msg | ) |
Set an error message.
public
| string |
Definition at line 767 of file Upload.php.
References $CI, get_instance(), and log_message().
Referenced by do_upload(), is_allowed_filetype(), set_filename(), and validate_upload_path().
00768 { 00769 $CI =& get_instance(); 00770 $CI->lang->load('upload'); 00771 00772 if (is_array($msg)) 00773 { 00774 foreach ($msg as $val) 00775 { 00776 $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val); 00777 $this->error_msg[] = $msg; 00778 log_message('error', $msg); 00779 } 00780 } 00781 else 00782 { 00783 $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg); 00784 $this->error_msg[] = $msg; 00785 log_message('error', $msg); 00786 } 00787 }


| CI_Upload::set_filename | ( | $ | path, | |
| $ | filename | |||
| ) |
Set the file name.
This function takes a filename/path as input and looks for the existence of a file with the same name. If found, it will append a number to the end of the filename to avoid overwriting a pre-existing file.
public
| string | ||
| string |
Definition at line 345 of file Upload.php.
References set_error().
Referenced by do_upload().
00346 { 00347 if ($this->encrypt_name == TRUE) 00348 { 00349 mt_srand(); 00350 $filename = md5(uniqid(mt_rand())).$this->file_ext; 00351 } 00352 00353 if ( ! file_exists($path.$filename)) 00354 { 00355 return $filename; 00356 } 00357 00358 $filename = str_replace($this->file_ext, '', $filename); 00359 00360 $new_filename = ''; 00361 for ($i = 1; $i < 100; $i++) 00362 { 00363 if ( ! file_exists($path.$filename.$i.$this->file_ext)) 00364 { 00365 $new_filename = $filename.$i.$this->file_ext; 00366 break; 00367 } 00368 } 00369 00370 if ($new_filename == '') 00371 { 00372 $this->set_error('upload_bad_filename'); 00373 return FALSE; 00374 } 00375 else 00376 { 00377 return $new_filename; 00378 } 00379 }


| CI_Upload::set_image_properties | ( | $ | path = '' |
) |
Set Image Properties.
Uses GD to determine the width/height/type of image
public
| string |
Definition at line 448 of file Upload.php.
References is_image().
Referenced by do_upload().
00449 { 00450 if ( ! $this->is_image()) 00451 { 00452 return; 00453 } 00454 00455 if (function_exists('getimagesize')) 00456 { 00457 if (FALSE !== ($D = @getimagesize($path))) 00458 { 00459 $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png'); 00460 00461 $this->image_width = $D['0']; 00462 $this->image_height = $D['1']; 00463 $this->image_type = ( ! isset($types[$D['2']])) ? 'unknown' : $types[$D['2']]; 00464 $this->image_size_str = $D['3']; // string containing height and width 00465 } 00466 } 00467 }


| CI_Upload::set_max_filesize | ( | $ | n | ) |
Set Maximum File Size.
public
| integer |
Definition at line 390 of file Upload.php.
| CI_Upload::set_max_height | ( | $ | n | ) |
Set Maximum Image Height.
public
| integer |
Definition at line 418 of file Upload.php.
| CI_Upload::set_max_width | ( | $ | n | ) |
Set Maximum Image Width.
public
| integer |
Definition at line 404 of file Upload.php.
| CI_Upload::set_upload_path | ( | $ | path | ) |
| CI_Upload::set_xss_clean | ( | $ | flag = FALSE |
) |
Set XSS Clean.
Enables the XSS flag so that the file that was uploaded will be run through the XSS filter.
public
| bool |
Definition at line 481 of file Upload.php.
| CI_Upload::validate_upload_path | ( | ) |
Validate Upload Path.
Verifies that it is a valid upload path with proper permissions.
public
Definition at line 626 of file Upload.php.
References is_really_writable(), and set_error().
Referenced by do_upload().
00627 { 00628 if ($this->upload_path == '') 00629 { 00630 $this->set_error('upload_no_filepath'); 00631 return FALSE; 00632 } 00633 00634 if (function_exists('realpath') AND @realpath($this->upload_path) !== FALSE) 00635 { 00636 $this->upload_path = str_replace("\\", "/", realpath($this->upload_path)); 00637 } 00638 00639 if ( ! @is_dir($this->upload_path)) 00640 { 00641 $this->set_error('upload_no_filepath'); 00642 return FALSE; 00643 } 00644 00645 if ( ! is_really_writable($this->upload_path)) 00646 { 00647 $this->set_error('upload_not_writable'); 00648 return FALSE; 00649 } 00650 00651 $this->upload_path = preg_replace("/(.+?)\/*$/", "\\1/", $this->upload_path); 00652 return TRUE; 00653 }


| CI_Upload::$allowed_types = "" |
Definition at line 32 of file Upload.php.
| CI_Upload::$encrypt_name = FALSE |
Definition at line 41 of file Upload.php.
| CI_Upload::$error_msg = array() |
Definition at line 47 of file Upload.php.
| CI_Upload::$file_ext = "" |
Definition at line 38 of file Upload.php.
| CI_Upload::$file_name = "" |
Definition at line 34 of file Upload.php.
| CI_Upload::$file_size = "" |
Definition at line 37 of file Upload.php.
| CI_Upload::$file_temp = "" |
Definition at line 33 of file Upload.php.
| CI_Upload::$file_type = "" |
Definition at line 36 of file Upload.php.
| CI_Upload::$image_height = '' |
Definition at line 44 of file Upload.php.
| CI_Upload::$image_size_str = '' |
Definition at line 46 of file Upload.php.
| CI_Upload::$image_type = '' |
Definition at line 45 of file Upload.php.
| CI_Upload::$image_width = '' |
Definition at line 43 of file Upload.php.
| CI_Upload::$is_image = FALSE |
Definition at line 42 of file Upload.php.
| CI_Upload::$max_height = 0 |
Definition at line 31 of file Upload.php.
| CI_Upload::$max_size = 0 |
Definition at line 29 of file Upload.php.
| CI_Upload::$max_width = 0 |
Definition at line 30 of file Upload.php.
| CI_Upload::$mimes = array() |
| CI_Upload::$orig_name = "" |
Definition at line 35 of file Upload.php.
| CI_Upload::$overwrite = FALSE |
Definition at line 40 of file Upload.php.
| CI_Upload::$remove_spaces = TRUE |
Definition at line 49 of file Upload.php.
| CI_Upload::$temp_prefix = "temp_file_" |
Definition at line 51 of file Upload.php.
| CI_Upload::$upload_path = "" |
Definition at line 39 of file Upload.php.
| CI_Upload::$xss_clean = FALSE |
Definition at line 50 of file Upload.php.