0

0

codeigniter多文件上传使用示例_PHP教程

php中文网

php中文网

发布时间:2016-07-13 10:38:58

|

906人浏览过

|

来源于php中文网

原创

 这篇文章主要介绍了codeigniter多文件上传使用示例,需要的朋友可以参考下

腾讯交互翻译
腾讯交互翻译

腾讯AI Lab发布的一款AI辅助翻译产品

下载
代码如下: 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 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() checks       */        if(DIRECTORY_SEPARATOR !== "\"){         $cmd = "file --brief --mime ".escapeshellarg($tmp_name)." 2>&1";          if(function_exists("exec")){         /* This might look confusing, as $mime is 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 be set for $mime previously. This effectively makes the second parameter a dummy          * value, which is 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(strlen($mime) > 0){         $mime = explode(" ", trim($mime));         if(preg_match($regexp, $mime[(count($mime) - 1)], $matches)){          $this->file_type = $matches[1];          return;         }        }       }         if(function_exists("popen")){        $proc = @popen($cmd, "r");        if(is_resource($proc)){         $mime = @fread($proc, 512);         @pclose($proc);         if($mime !== FALSE){          $mime = explode(" ", trim($mime));          if(preg_match($regexp, $mime[(count($mime) - 1)], $matches)){           $this->file_type = $matches[1];           return;          }         }        }       }         //Fall back to the deprecated mime_content_type(), if available (still better than $_FILES[$field]["type"])       if(function_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"   => $this->upload_path.$this->file_name,       "raw_name"   => str_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" => $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 will already be set by validate_upload_path() so just return FALSE       return FALSE;      }        //Every file will have 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; $iset_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 was 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 was 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 have 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 an 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_filename_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 an 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        * be 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 two 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 was 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;    }   }    

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/733185.htmlTechArticle这篇文章主要介绍了codeigniter多文件上传使用示例,需要的朋友可以参考下 代码如下:?php if(!defined(BASEPATH)){ exit(No direct script access allowed); }/...

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
C# ASP.NET Core微服务架构与API网关实践
C# ASP.NET Core微服务架构与API网关实践

本专题围绕 C# 在现代后端架构中的微服务实践展开,系统讲解基于 ASP.NET Core 构建可扩展服务体系的核心方法。内容涵盖服务拆分策略、RESTful API 设计、服务间通信、API 网关统一入口管理以及服务治理机制。通过真实项目案例,帮助开发者掌握构建高可用微服务系统的关键技术,提高系统的可扩展性与维护效率。

76

2026.03.11

Go高并发任务调度与Goroutine池化实践
Go高并发任务调度与Goroutine池化实践

本专题围绕 Go 语言在高并发任务处理场景中的实践展开,系统讲解 Goroutine 调度模型、Channel 通信机制以及并发控制策略。内容包括任务队列设计、Goroutine 池化管理、资源限制控制以及并发任务的性能优化方法。通过实际案例演示,帮助开发者构建稳定高效的 Go 并发任务处理系统,提高系统在高负载环境下的处理能力与稳定性。

38

2026.03.10

Kotlin Android模块化架构与组件化开发实践
Kotlin Android模块化架构与组件化开发实践

本专题围绕 Kotlin 在 Android 应用开发中的架构实践展开,重点讲解模块化设计与组件化开发的实现思路。内容包括项目模块拆分策略、公共组件封装、依赖管理优化、路由通信机制以及大型项目的工程化管理方法。通过真实项目案例分析,帮助开发者构建结构清晰、易扩展且维护成本低的 Android 应用架构体系,提升团队协作效率与项目迭代速度。

83

2026.03.09

JavaScript浏览器渲染机制与前端性能优化实践
JavaScript浏览器渲染机制与前端性能优化实践

本专题围绕 JavaScript 在浏览器中的执行与渲染机制展开,系统讲解 DOM 构建、CSSOM 解析、重排与重绘原理,以及关键渲染路径优化方法。内容涵盖事件循环机制、异步任务调度、资源加载优化、代码拆分与懒加载等性能优化策略。通过真实前端项目案例,帮助开发者理解浏览器底层工作原理,并掌握提升网页加载速度与交互体验的实用技巧。

97

2026.03.06

Rust内存安全机制与所有权模型深度实践
Rust内存安全机制与所有权模型深度实践

本专题围绕 Rust 语言核心特性展开,深入讲解所有权机制、借用规则、生命周期管理以及智能指针等关键概念。通过系统级开发案例,分析内存安全保障原理与零成本抽象优势,并结合并发场景讲解 Send 与 Sync 特性实现机制。帮助开发者真正理解 Rust 的设计哲学,掌握在高性能与安全性并重场景中的工程实践能力。

223

2026.03.05

PHP高性能API设计与Laravel服务架构实践
PHP高性能API设计与Laravel服务架构实践

本专题围绕 PHP 在现代 Web 后端开发中的高性能实践展开,重点讲解基于 Laravel 框架构建可扩展 API 服务的核心方法。内容涵盖路由与中间件机制、服务容器与依赖注入、接口版本管理、缓存策略设计以及队列异步处理方案。同时结合高并发场景,深入分析性能瓶颈定位与优化思路,帮助开发者构建稳定、高效、易维护的 PHP 后端服务体系。

458

2026.03.04

AI安装教程大全
AI安装教程大全

2026最全AI工具安装教程专题:包含各版本AI绘图、AI视频、智能办公软件的本地化部署手册。全篇零基础友好,附带最新模型下载地址、一键安装脚本及常见报错修复方案。每日更新,收藏这一篇就够了,让AI安装不再报错!

169

2026.03.04

Swift iOS架构设计与MVVM模式实战
Swift iOS架构设计与MVVM模式实战

本专题聚焦 Swift 在 iOS 应用架构设计中的实践,系统讲解 MVVM 模式的核心思想、数据绑定机制、模块拆分策略以及组件化开发方法。内容涵盖网络层封装、状态管理、依赖注入与性能优化技巧。通过完整项目案例,帮助开发者构建结构清晰、可维护性强的 iOS 应用架构体系。

246

2026.03.03

C++高性能网络编程与Reactor模型实践
C++高性能网络编程与Reactor模型实践

本专题围绕 C++ 在高性能网络服务开发中的应用展开,深入讲解 Socket 编程、多路复用机制、Reactor 模型设计原理以及线程池协作策略。内容涵盖 epoll 实现机制、内存管理优化、连接管理策略与高并发场景下的性能调优方法。通过构建高并发网络服务器实战案例,帮助开发者掌握 C++ 在底层系统与网络通信领域的核心技术。

34

2026.03.03

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
CodeIgniter3(CI3)基础教程(全)
CodeIgniter3(CI3)基础教程(全)

共37课时 | 4.9万人学习

Codeigniter 3 中文开发手册
Codeigniter 3 中文开发手册

共0课时 | 0人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号