<?php if(!defined("BASEPATH")){ exit("No direct script access allowed"); }</P><P> /**<BR> * Multi-Upload<BR> * <BR> * Extends CodeIgniters native Upload class to add support for multiple<BR> * uploads.<BR> *<BR> * @package CodeIgniter<BR> * @subpackage Libraries<BR> * @category Uploads<BR> */<BR> class MY_Upload extends CI_Upload {</P><P><BR> /**<BR> * Properties<BR> */<BR> protected $_multi_upload_data = array();<BR> protected $_multi_file_name_override = "";</P><P><BR> /**<BR> * Initialize preferences<BR> *<BR> * @access public<BR> * @param array<BR> * @return void<BR> */<BR> public function initialize($config = array()){<BR> //Upload default settings.<BR> $defaults = array(<BR> "max_size" => 0,<BR> "max_width" => 0,<BR> "max_height" => 0,<BR> "max_filename" => 0,<BR> "allowed_types" => "",<BR> "file_temp" => "",<BR> "file_name" => "",<BR> "orig_name" => "",<BR> "file_type" => "",<BR> "file_size" => "",<BR> "file_ext" => "",<BR> "upload_path" => "",<BR> "overwrite" => FALSE,<BR> "encrypt_name" => FALSE,<BR> "is_image" => FALSE,<BR> "image_width" => "",<BR> "image_height" => "",<BR> "image_type" => "",<BR> "image_size_str" => "",<BR> "error_msg" => array(),<BR> "mimes" => array(),<BR> "remove_spaces" => TRUE,<BR> "xss_clean" => FALSE,<BR> "temp_prefix" => "temp_file_",<BR> "client_name" => ""<BR> );</P><P> //Set each configuration.<BR> foreach($defaults as $key => $val){<BR> if(isset($config[$key])){<BR> $method = "set_{$key}";<BR> if(method_exists($this, $method)){<BR> $this->$method($config[$key]);<BR> } else {<BR> $this->$key = $config[$key];<BR> }<BR> } else {<BR> $this->$key = $val;<BR> }<BR> }</P><P> //Check if file_name was provided.<BR> if(!empty($this->file_name)){<BR> //Multiple file upload.<BR> if(is_array($this->file_name)){<BR> //Clear file name override.<BR> $this->_file_name_override = "";</P><P> //Set multiple file name override.<BR> $this->_multi_file_name_override = $this->file_name;<BR> //Single file upload.<BR> } else {<BR> //Set file name override.<BR> $this->_file_name_override = $this->file_name;</P><P> //Clear multiple file name override.<BR> $this->_multi_file_name_override = "";<BR> }<BR> }<BR> }</P><P><BR> /**<BR> * File MIME Type<BR> * <BR> * Detects the (actual) MIME type of the uploaded file, if possible.<BR> * The input array is expected to be $_FILES[$field].<BR> * <BR> * In the case of multiple uploads, a optional second argument may be<BR> * passed specifying which array element of the $_FILES[$field] array<BR> * elements should be referenced (name, type, tmp_name, etc).<BR> *<BR> * @access protected<BR> * @param $file array<BR> * @param $count int<BR> * @return void<BR> */<BR> protected function _file_mime_type($file, $count=0){<BR> //Mutliple file?<BR> if(is_array($file["name"])){<BR> $tmp_name = $file["tmp_name"][$count];<BR> $type = $file["type"][$count];<BR> //Single file.<BR> } else {<BR> $tmp_name = $file["tmp_name"];<BR> $type = $file["type"];<BR> }</P><P> //We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii).<BR> $regexp = "/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/";</P><P> /* Fileinfo Extension - most reliable method.<BR> * <BR> * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the<BR> * more convenient FILEINFO_MIME_TYPE flag doesn't exist.<BR> */<BR> if(function_exists("finfo_file")){<BR> $finfo = finfo_open(FILEINFO_MIME);<BR> if(is_resource($finfo)){<BR> $mime = @finfo_file($finfo, $tmp_name);<BR> finfo_close($finfo);</P><P> /* According to the comments section of the PHP manual page,<BR> * it is possible that this function returns an empty string<BR> * for some files (e.g. if they don't exist in the magic MIME database).<BR> */<BR> if(is_string($mime) && preg_match($regexp, $mime, $matches)){<BR> $this->file_type = $matches[1];<BR> return;<BR> }<BR> }<BR> }</P><P> /* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type,<BR> * which is still more secure than depending on the value of $_FILES[$field]['type'], and as it<BR> * was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better<BR> * than mime_content_type() as well, hence the attempts to try calling the command line with<BR> * three different functions.<BR> *<BR> * Notes:<BR> * - the DIRECTORY_SEPARATOR comparison ensures that we're not on a Windows system<BR> * - many system admins would disable the exec(), shell_exec(), popen() and similar functions<BR> * due to security concerns, hence the function_exists() checks<BR> */<BR> if(DIRECTORY_SEPARATOR !== "\\"){<BR> $cmd = "file --brief --mime ".escapeshellarg($tmp_name)." 2>&1";</P><P> if(function_exists("exec")){<BR> /* This might look confusing, as $mime is being populated with all of the output when set in the second parameter.<BR> * However, we only neeed the last line, which is the actual return value of exec(), and as such - it overwrites<BR> * anything that could already be set for $mime previously. This effectively makes the second parameter a dummy<BR> * value, which is only put to allow us to get the return status code.<BR> */<BR> $mime = @exec($cmd, $mime, $return_status);<BR> if($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches)){<BR> $this->file_type = $matches[1];<BR> return;<BR> }<BR> }<BR> }</P><P> if((bool)@ini_get("safe_mode") === FALSE && function_exists("shell_exec")){<BR> $mime = @shell_exec($cmd);<BR> if(strlen($mime) > 0){<BR> $mime = explode("\n", trim($mime));<BR> if(preg_match($regexp, $mime[(count($mime) - 1)], $matches)){<BR> $this->file_type = $matches[1];<BR> return;<BR> }<BR> }<BR> }</P><P> if(function_exists("popen")){<BR> $proc = @popen($cmd, "r");<BR> if(is_resource($proc)){<BR> $mime = @fread($proc, 512);<BR> @pclose($proc);<BR> if($mime !== FALSE){<BR> $mime = explode("\n", trim($mime));<BR> if(preg_match($regexp, $mime[(count($mime) - 1)], $matches)){<BR> $this->file_type = $matches[1];<BR> return;<BR> }<BR> }<BR> }<BR> }</P><P> //Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field]["type"])<BR> if(function_exists("mime_content_type")){<BR> $this->file_type = @mime_content_type($tmp_name);<BR> //It's possible that mime_content_type() returns FALSE or an empty string.<BR> if(strlen($this->file_type) > 0){<BR> return;<BR> }<BR> }</P><P> //If all else fails, use $_FILES default mime type.<BR> $this->file_type = $type;<BR> }</P><P><BR> /**<BR> * Set Multiple Upload Data<BR> *<BR> * @access protected<BR> * @return void<BR> */<BR> protected function set_multi_upload_data(){<BR> $this->_multi_upload_data[] = array(<BR> "file_name" => $this->file_name,<BR> "file_type" => $this->file_type,<BR> "file_path" => $this->upload_path,<BR> "full_path" => $this->upload_path.$this->file_name,<BR> "raw_name" => str_replace($this->file_ext, "", $this->file_name),<BR> "orig_name" => $this->orig_name,<BR> "client_name" => $this->client_name,<BR> "file_ext" => $this->file_ext,<BR> "file_size" => $this->file_size,<BR> "is_image" => $this->is_image(),<BR> "image_width" => $this->image_width,<BR> "image_height" => $this->image_height,<BR> "image_type" => $this->image_type,<BR> "image_size_str" => $this->image_size_str<BR> );<BR> }</P><P><BR> /**<BR> * Get Multiple Upload Data<BR> *<BR> * @access public<BR> * @return array<BR> */<BR> public function get_multi_upload_data(){<BR> return $this->_multi_upload_data;<BR> }</P><P><BR> /**<BR> * Multile File Upload<BR> *<BR> * @access public<BR> * @param string<BR> * @return mixed<BR> */<BR> public function do_multi_upload($field){<BR> //Is $_FILES[$field] set? If not, no reason to continue.<BR> if(!isset($_FILES[$field])){ return false; }</P><P> //Is this really a multi upload?<BR> if(!is_array($_FILES[$field]["name"])){<BR> //Fallback to do_upload method.<BR> return $this->do_upload($field);<BR> }</P><P> //Is the upload path valid?<BR> if(!$this->validate_upload_path()){<BR> //Errors will already be set by validate_upload_path() so just return FALSE<BR> return FALSE;<BR> }</P><P> //Every file will have a separate entry in each of the $_FILES associative array elements (name, type, etc).<BR> //Loop through $_FILES[$field]["name"] as representative of total number of files. Use count as key in<BR> //corresponding elements of the $_FILES[$field] elements.<BR> for($i=0; $i<count($_FILES[$field]["name"]); $i++){<BR> //Was the file able to be uploaded? If not, determine the reason why.<BR> if(!is_uploaded_file($_FILES[$field]["tmp_name"][$i])){<BR> //Determine error number.<BR> $error = (!isset($_FILES[$field]["error"][$i])) ? 4 : $_FILES[$field]["error"][$i];</P><P> //Set error.<BR> switch($error){<BR> //UPLOAD_ERR_INI_SIZE<BR> case 1:<BR> $this->set_error("upload_file_exceeds_limit");<BR> break;<BR> //UPLOAD_ERR_FORM_SIZE<BR> case 2:<BR> $this->set_error("upload_file_exceeds_form_limit");<BR> break;<BR> //UPLOAD_ERR_PARTIAL<BR> case 3:<BR> $this->set_error("upload_file_partial");<BR> break;<BR> //UPLOAD_ERR_NO_FILE<BR> case 4:<BR> $this->set_error("upload_no_file_selected");<BR> break;<BR> //UPLOAD_ERR_NO_TMP_DIR<BR> case 6:<BR> $this->set_error("upload_no_temp_directory");<BR> break;<BR> //UPLOAD_ERR_CANT_WRITE<BR> case 7:<BR> $this->set_error("upload_unable_to_write_file");<BR> break;<BR> //UPLOAD_ERR_EXTENSION<BR> case 8:<BR> $this->set_error("upload_stopped_by_extension");<BR> break;<BR> default:<BR> $this->set_error("upload_no_file_selected");<BR> break;<BR> }</P><P> //Return failed upload.<BR> return FALSE;<BR> }</P><P> //Set current file data as class variables.<BR> $this->file_temp = $_FILES[$field]["tmp_name"][$i];<BR> $this->file_size = $_FILES[$field]["size"][$i];<BR> $this->_file_mime_type($_FILES[$field], $i);<BR> $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $this->file_type);<BR> $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));<BR> $this->file_name = $this->_prep_filename($_FILES[$field]["name"][$i]);<BR> $this->file_ext = $this->get_extension($this->file_name);<BR> $this->client_name = $this->file_name;</P><P> //Is the file type allowed to be uploaded?<BR> if(!$this->is_allowed_filetype()){<BR> $this->set_error("upload_invalid_filetype");<BR> return FALSE;<BR> }</P><P> //If we're overriding, let's now make sure the new name and type is allowed.<BR> //Check if a filename was supplied for the current file. Otherwise, use it's given name.<BR> if(!empty($this->_multi_file_name_override[$i])){<BR> $this->file_name = $this->_prep_filename($this->_multi_file_name_override[$i]);</P><P> //If no extension was provided in the file_name config item, use the uploaded one.<BR> if(strpos($this->_multi_file_name_override[$i], ".") === FALSE){<BR> $this->file_name .= $this->file_ext;<BR> //An extension was provided, lets have it!<BR> } else {<BR> $this->file_ext = $this->get_extension($this->_multi_file_name_override[$i]);<BR> }</P><P> if(!$this->is_allowed_filetype(TRUE)){<BR> $this->set_error("upload_invalid_filetype");<BR> return FALSE;<BR> }<BR> }</P><P> //Convert the file size to kilobytes.<BR> if($this->file_size > 0){<BR> $this->file_size = round($this->file_size/1024, 2);<BR> }</P><P> //Is the file size within the allowed maximum?<BR> if(!$this->is_allowed_filesize()){<BR> $this->set_error("upload_invalid_filesize");<BR> return FALSE;<BR> }</P><P> //Are the image dimensions within the allowed size?<BR> //Note: This can fail if the server has an open_basdir restriction.<BR> if(!$this->is_allowed_dimensions()){<BR> $this->set_error("upload_invalid_dimensions");<BR> return FALSE;<BR> }</P><P> //Sanitize the file name for security.<BR> $this->file_name = $this->clean_file_name($this->file_name);</P><P> //Truncate the file name if it's too long<BR> if($this->max_filename > 0){<BR> $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);<BR> }</P><P> //Remove white spaces in the name<BR> if($this->remove_spaces == TRUE){<BR> $this->file_name = preg_replace("/\s+/", "_", $this->file_name);<BR> }</P><P> /* Validate the file name<BR> * This function appends an number onto the end of<BR> * the file if one with the same name already exists.<BR> * If it returns false there was a problem.<BR> */<BR> $this->orig_name = $this->file_name;<BR> if($this->overwrite == FALSE){<BR> $this->file_name = $this->set_filename($this->upload_path, $this->file_name);<BR> if($this->file_name === FALSE){<BR> return FALSE;<BR> }<BR> }</P><P> /* Run the file through the XSS hacking filter<BR> * This helps prevent malicious code from being<BR> * embedded within a file. Scripts can easily<BR> * be disguised as images or other file types.<BR> */<BR> if($this->xss_clean){<BR> if($this->do_xss_clean() === FALSE){<BR> $this->set_error("upload_unable_to_write_file");<BR> return FALSE;<BR> }<BR> }</P><P> /* Move the file to the final destination<BR> * To deal with different server configurations<BR> * we'll attempt to use copy() first. If that fails<BR> * we'll use move_uploaded_file(). One of the two sho<div>)本文来源gaodai.ma#com搞#代!码网_</div><strong>搞代gaodaima码</strong>uld<BR> * reliably work in most environments<BR> */<BR> if(!@copy($this->file_temp, $this->upload_path.$this->file_name)){<BR> if(!@move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name)){<BR> $this->set_error("upload_destination_error");<BR> return FALSE;<BR> }<BR> }</P><P> /* Set the finalized image dimensions<BR> * This sets the image width/height (assuming the<BR> * file was an image). We use this information<BR> * in the "data" function.<BR> */<BR> $this->set_image_properties($this->upload_path.$this->file_name);</P><P> //Set current file data to multi_file_upload_data.<BR> $this->set_multi_upload_data();<BR> }</P><P> //Return all file upload data.<BR> return TRUE;<BR> }<BR> }<BR>