這篇文章主要介紹了codeigniter多文件上傳使用示例,需要的朋友可以參考下
代碼如下: <?php if(!defined("BASEPATH")){ exit("No direct script access allowed"); } /** * Multi-Upload * * Extends CodeIgniters native Upload class to add support for multiple * uploads. * * @package CodeIgniter * @subpackage Libraries * @category Uploads */ class MY_Upload extends CI_Upload { /** * Properties */ protected $_multi_upload_data = array(); protected $_multi_file_name_override = ""; /** * Initialize preferences * * @access public * @param array * @return void */ public function initialize($config = array()){ //Upload default settings. $defaults = array( "max_size" => 0, "max_width" => 0, "max_height" => 0, "max_filename" => 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_", "client_name" => "" ); //Set each configuration. foreach($defaults as $key => $val){ if(isset($config[$key])){ $method = "set_{$key}"; if(method_exists($this, $method)){ $this->$method($config[$key]); } else { $this->$key = $config[$key]; } } else { $this->$key = $val; } } //Check if file_name was provided. if(!empty($this->file_name)){ //Multiple file upload. if(is_array($this->file_name)){ //Clear file name override. $this->_file_name_override = ""; //Set multiple file name override. $this->_multi_file_name_override = $this->file_name; //Single file upload. } else { //Set file name override. $this->_file_name_override = $this->file_name; //Clear multiple file name override. $this->_multi_file_name_override = ""; } } } /** * File MIME Type * * Detects the (actual) MIME type of the uploaded file, if possible. * The input array is expected to be $_FILES[$field]. * * In the case of multiple uploads, a optional second argument may be * passed specifying which array element of the $_FILES[$field] array * elements should be referenced (name, type, tmp_name, etc). * * @access protected * @param $file array * @param $count int * @return void */ protected function _file_mime_type($file, $count=0){ //Mutliple file? if(is_array($file["name"])){ $tmp_name = $file["tmp_name"][$count]; $type = $file["type"][$count]; //Single file. } else { $tmp_name = $file["tmp_name"]; $type = $file["type"]; } //We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii). $regexp = "/^([a-z-]+/[a-z0-9-.+]+)(;s.+)?$/"; /* Fileinfo Extension - most reliable method. * * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the * more convenient FILEINFO_MIME_TYPE flag doesn't exist. */ if(function_exists("finfo_file")){ $finfo = finfo_open(FILEINFO_MIME); if(is_resource($finfo)){ $mime = @finfo_file($finfo, $tmp_name); finfo_close($finfo); /* According to the comments section of the PHP manual page, * it is possible that this function returns an empty string * for some files (e.g. if they don't exist in the magic MIME database). */ if(is_string($mime) && preg_match($regexp, $mime, $matches)){ $this->file_type = $matches[1]; return; } } } /* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type, * which is still more secure than depending on the value of $_FILES[$field]['type'], and as it * was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better * than mime_content_type() as well, hence the attempts to try calling the command line with * three different functions. * * Notes: * - the DIRECTORY_SEPARATOR compari