From 2f26ba98c2ccda07fe2177d0b48b117ca8fddafe Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Sat, 4 Oct 2008 01:38:01 +0000 Subject: Added function to optionally limit the length of the file name --- system/libraries/Upload.php | 57 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) (limited to 'system/libraries/Upload.php') diff --git a/system/libraries/Upload.php b/system/libraries/Upload.php index 13026bcac..a2437d072 100644 --- a/system/libraries/Upload.php +++ b/system/libraries/Upload.php @@ -29,6 +29,7 @@ class CI_Upload { var $max_size = 0; var $max_width = 0; var $max_height = 0; + var $max_filename = 0; var $allowed_types = ""; var $file_temp = ""; var $file_name = ""; @@ -147,7 +148,7 @@ class CI_Upload { // errors will already be set by validate_upload_path() so just return FALSE return FALSE; } - + // Was the file able to be uploaded? If not, determine the reason why. if ( ! is_uploaded_file($_FILES[$field]['tmp_name'])) { @@ -221,6 +222,12 @@ class CI_Upload { // Sanitize the file name for security $this->file_name = $this->clean_file_name($this->file_name); + + // Truncate the file name if it's too long + if ($this->max_filename > 0) + { + $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename); + } // Remove white spaces in the name if ($this->remove_spaces == TRUE) @@ -325,7 +332,8 @@ class CI_Upload { */ function set_upload_path($path) { - $this->upload_path = $path; + // Make sure it has a trailing slash + $this->upload_path = rtrim($path, '/').'/'; } // -------------------------------------------------------------------- @@ -347,7 +355,7 @@ class CI_Upload { if ($this->encrypt_name == TRUE) { mt_srand(); - $filename = md5(uniqid(mt_rand())).$this->file_ext; + $filename = md5(uniqid(mt_rand())).$this->file_ext; } if ( ! file_exists($path.$filename)) @@ -394,6 +402,20 @@ class CI_Upload { // -------------------------------------------------------------------- + /** + * Set Maximum File Name Length + * + * @access public + * @param integer + * @return void + */ + function set_max_filename($n) + { + $this->max_filename = ( ! eregi("^[[:digit:]]+$", $n)) ? 0 : $n; + } + + // -------------------------------------------------------------------- + /** * Set Maximum Image Width * @@ -711,7 +733,34 @@ class CI_Upload { return stripslashes($filename); } + + // -------------------------------------------------------------------- + /** + * Limit the File Name Length + * + * @access public + * @param string + * @return string + */ + function limit_filename_length($filename, $length) + { + if (strlen($filename) < $length) + { + return $filename; + } + + $ext = ''; + if (strpos($filename, '.') !== FALSE) + { + $parts = explode('.', $filename); + $ext = '.'.array_pop($parts); + $filename = implode('.', $parts); + } + + return substr($filename, 0, ($length - strlen($ext))).$ext; + } + // -------------------------------------------------------------------- /** @@ -832,6 +881,8 @@ class CI_Upload { return ( ! isset($this->mimes[$mime])) ? FALSE : $this->mimes[$mime]; } + // -------------------------------------------------------------------- + /** * Prep Filename * -- cgit v1.2.3-24-g4f1b