CodeIgniter multi-File upload using sample _php tutorial

Source: Internet
Author: User
Tags codeigniter
This article mainly describes the CodeIgniter multi-file Upload use example, the need for friends can refer to the following

The code is as follows: 0, "max_width" = 0, "max_height" 0, "Max_filename" + 0, "allowed_types" and "", "File_temp" and "", "fi Le_name "+" "," orig_name "=" "," File_type "and" "," File_size "and" "," File_ext "and" "," Upload_path "=& Gt "", "overwrite" = False, "encrypt_name" = False, "is_image" = False, "image_width" = "", "Image_height" =& Gt "", "Image_type" and "" "," image_size_str "=" "," error_msg "= = Array ()," mimes "= = Array (), Remove_spaces =& Gt TRUE, "xss_clean" = FALSE, "temp_prefix" = "Temp_file_", "Client_name" and "" "; 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 Overrid E. $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 could be * passed specifying which array element of the $_fi les[$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's 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 was an ugly hack, but Unix-type systems provide a "native" the "from" to detect the file type, * which was still more sec Ure 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_ty PE () as well, hence the attempts-try calling the command line with * three different functions. * * Notes: *-The Directory_separator comparison ensures that we ' re not on a Windows system *-many system admins would Disable the exec (), shell_exec (), Popen () and similar functions * due to security concerns, hence the function_exists () ch Ecks */if (directory_separator!== "\ \") {$cmd = "file--brief--mime". Escapeshellarg ($tmp _name). " 2>&1 "; if (function_exists ("exec")) {/* This might look confusing, as $mime are being populated with all of the output when set in The second parameter. * However, we only neeed the last line, which is the actual return value of exec (), and as such-it overwrites * anything That could already is set for $mime previously. This effectively makes the second parameter a dummy * value, which are only put to allow us to get the RetuRN status code. */$mime = @exec ($cmd, $mime, $return _status); if ($return _status = = = 0 && is_string ($mime) && preg_match ($regexp, $mime, $matches)) {$this->file_ Type = $matches [1]; Return }}} if ((bool) @ini_get ("safe_mode") = = = FALSE && function_exists ("Shell_exec")) {$mime = @shell_exec ($cmd); if (s Trlen ($mime) > 0) {$mime = explode ("\ n", Trim ($mime)), if (Preg_match ($regexp, $mime [(Count ($mime)-1)], $matches)) {$t His->file_type = $matches [1]; Return }}} if (Function_exists ("Popen")) {$proc = @popen ($cmd, "R"), if (Is_resource ($proc)) {$mime = @fread ($proc); @pclose ($proc); if ($mime!== FALSE) {$mime = explode ("\ n", Trim ($mime)), if (Preg_match ($regexp, $mime [(Count ($mime)-1], $matches)) {$th Is->file_type = $matches [1]; Return }}}}//fall back to the deprecated mime_content_type (), if available (still better than $_files[$field ["type"]) if (fun Ction_exists ("Mime_content_type")) {$this->file_type = @mime_content_type ($tmp _name);//it' s possible that Mime_content_type () returns FALSE or an empty string. if (strlen ($this->file_type) > 0) {return;}} If all else fails, use $_files default MIME type. $this->file_type = $type; }/** * Set multiple Upload Data * * @access protected * @return void */protected function Set_multi_upload_data () {$this ->_multi_upload_data[] = Array ("file_name" + $this->file_name, "File_type" +-$this->file_type, "File_ Path "= $this->upload_path," Full_path "and" Raw_name "-$this->upload_path. Replace ($this->file_ext, "", $this->file_name), "orig_name" = $this->orig_name, "client_name" = $ This->client_name, "file_ext" = $this->file_ext, "file_size" +-$this->file_size, "is_image" = $ This->is_image (), "image_width" = $this->image_width, "image_height" +-$this->image_height, "Image_ Type "= $this->image_type," image_size_str "and" = $this->image_size_str); } /** * Get multiple Upload Data * * @access public * @return Array */Public function Get_multi_upload_data () {return $this _multi_upload_data; }/** * Multile File Upload * * @access public * @param String * @return Mixed */Public Function do_multi_upload ($field) { is $_files[$field] set? If not, no reason to continue. if (!isset ($_files[$field])) {return false;}//is this really a multi upload? if (!is_array ($_files[$field ["name"]) {//fallback to Do_upload method. Return $this->do_upload ($field);}//is the Upload path valid? if (! $this->validate_upload_path ()) {//errors'll already be set by Validate_upload_path () so just return FALSE return FALSE; //every file'll has a separate entry in each of the $_files associative array elements (name, type, etc). Loop through $_files[$field ["name"] as representative of total number of FILES. Use count as key in//corresponding elements of the $_files[$field] elements. for ($i =0; $i Set_error ("Upload_file_exceeds_limit"); Break Upload_err_form_size Case 2: $this->set_error ("Upload_file_exceeds_form_limit"); Break Upload_err_partial Case 3: $this->set_error ("upload_file_partial"); Break Upload_err_no_file Case 4: $this->set_error ("upload_no_file_selected"); Break Upload_err_no_tmp_dir Case 6: $this->set_error ("Upload_no_temp_directory"); Break Upload_err_cant_write Case 7: $this->set_error ("Upload_unable_to_write_file"); Break Upload_err_extension Case 8: $this->set_error ("upload_stopped_by_extension"); Break Default: $this->set_error ("upload_no_file_selected"); Break }//return failed upload. return FALSE; }//set current file data as class variables. $this->file_temp = $_files[$field] ["Tmp_name"] [$i]; $this->file_size = $_files[$field ["Size"] [$i]; $this->_file_mime_type ($_files[$field], $i); $this->file_type = Preg_replace ("/^ (. +?);. *$/"," \\1 ", $this->file_type); $this->file_type = Strtolower (Trim (striPslashes ($this->file_type), ' "'); $this->file_name = $this->_prep_filename ($_files[$field ["name"] [$i]); $this->file_ext = $this->get_extension ($this->file_name); $this->client_name = $this->file_name; Is the file type allowed to be uploaded? if (! $this->is_allowed_filetype ()) {$this->set_error ("Upload_invalid_filetype"); return FALSE;}//if we ' re Overriding, let's now make sure the new name and type is allowed. Check if a filename is supplied for the current file. Otherwise, use it ' s given name. if (!empty ($this->_multi_file_name_override[$i])) {$this->file_name = $this->_prep_filename ($this->_ multi_file_name_override[$i]); If no extension is provided in the File_name Config item, use the uploaded one. if (Strpos ($this->_multi_file_name_override[$i], ".") = = = FALSE) {$this->file_name. = $this->file_ext;//an Extension was provided, lets has it! } else {$this->file_ext = $this->get_extension ($this->_multi_file_name_override[$i]); } if (! $this->is_allowed_filetype (TRUE)) {$this->set_error ("Upload_invalid_filetype"); return FALSE;}} Convert the file size to kilobytes. if ($this->file_size > 0) {$this->file_size = round ($this->file_size/1024, 2);}//is the file size within the Allowed maximum? if (! $this->is_allowed_filesize ()) {$this->set_error ("Upload_invalid_filesize"); return FALSE;}//are the image Dimensions within the allowed size? Note:this can fail if the server has a open_basdir restriction. if (! $this->is_allowed_dimensions ()) {$this->set_error ("upload_invalid_dimensions"); return FALSE;}// 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_filenam E_length ($this->file_name, $this->max_filename); }//remove white spaces in the name if ($this->remove_spaces = = TRUE) {$this->filE_name = Preg_replace ("/\s+/", "_", $this->file_name); }/* Validate the file name * This function appends a number onto the end of * The file if one with the same name already Exists. * IF It returns false there was a problem. */$this->orig_name = $this->file_name; if ($this->overwrite = = FALSE) {$this->file_name = $this->set_filename ($this->upload_path, $this->file _NAME); if ($this->file_name = = = False) {return false;}} /* Run the file through the XSS hacking filter * This helps prevent malicious code from being * embedded within a file. Scripts can easily * is disguised as images or other file types. */if ($this->xss_clean) {if ($this->do_xss_clean () = = = FALSE) {$this->set_error ("Upload_unable_to_write_ File "); return FALSE; }/*/Move the file to the final destination * to deal with different server configurations * We'll attempt to use copy () First. If that fails * We'll use Move_uploaded_file (). One of the should * reliably work in most enVironments */if (! @copy ($this->file_temp, $this->upload_path. $this->file_name)) {if (! @move_uploaded_file ( $this->file_temp, $this->upload_path $this->file_name) {$this->set_error ("Upload_destination_error") ); return FALSE; }}/* Set The finalized image dimensions * This sets the image width/height (assuming the * file is an image). We Use this information * in the "data" function. */$this->set_image_properties ($this->upload_path. $this->file_name); Set current file data to Multi_file_upload_data. $this->set_multi_upload_data (); }//return all file upload data. return TRUE; } }

http://www.bkjia.com/PHPjc/733185.html www.bkjia.com true http://www.bkjia.com/PHPjc/733185.html techarticle This article mainly describes the CodeIgniter multi-file Upload use example, the need for friends can refer to the following code:? PHP if (!defined (BasePath)) {exit (No Direct script access allowed);} /...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.