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 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("\n", 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("\n", 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; $i<count($_FILES[$field]["name"]); $i++){
      //Was the file able to be uploaded? If not, determine the reason why.
      if(!is_uploaded_file($_FILES[$field]["tmp_name"][$i])){
       //Determine error number.
       $error = (!isset($_FILES[$field]["error"][$i])) ? 4 : $_FILES[$field]["error"][$i];

//Set error.
       switch($error){
        //UPLOAD_ERR_INI_SIZE
        case 1:
         $this->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 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;
   }
  }

(0)

相关推荐

  • Codeigniter实现多文件上传并创建多个缩略图

    该程序可以实现:1.同时上传5张图片2.同时生成两种尺寸的缩略图3.保存到mysql controllers:upload.php文件: 复制代码 代码如下: <?phpclass Upload extends Controller {  function go() {    if(isset($_POST['go'])) {      //初始化      $config['upload_path'] = 'album/source';       $config['allowed_types

  • SWFUpload与CI不能正确上传识别文件MIME类型解决方法分享

    解决方案如下,其它框架雷同. 源代码(/system/libraries/upload.php 199 line) $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']); 修改成如下: 复制代码 代码如下: //Edit By Tacker if(function_exists('mime_content_type')){ $this->file_t

  • 解决Codeigniter不能上传rar和zip压缩包问题

    codeigniter在上传文件时对格式的限制是在application文件夹下config中的mimes.php文件中定义的.该文件默认不包含rar,而且对zip的定义不能上传压缩包.我们在类中,加入以下代码,即可解决这个问题: 复制代码 代码如下: "zip" => array("application/x-zip", "application/zip" ,"application/x-zip-compressed"

  • 2个Codeigniter文件批量上传控制器写法例子

    例子一: /** * 多文件上传 * * @author Dream <dream@shanjing-inc.com> */ public function multiple_uploads() { //载入所需类库 $this->load->library('upload'); //配置上传参数 $upload_config = array( 'upload_path' => '', 'allowed_types' => 'jpg|png|gif', 'max_siz

  • codeigniter上传图片不能正确识别图片类型问题解决方法

    在用 codeigniter 的上传类上传图片的时候,明明是 jpg 格式图片,但是 ci 始终识别成 application/octet-stream 类型,从而导致上传失败,搜集了下资料,解决方法如下: 在 php.ini 中开启 fileinfo 扩展来获取正确的类型: 复制代码 代码如下: //windows extension = php_fileinfo.dll //linux extension = fileinfo.so 注:关于fileinfo PHP官方推荐mime_cont

  • 使用CodeIgniter的类库做图片上传

    CodeIgniter的文件上传类允许文件被上传.您可以设置指定上传某类型的文件及指定大小的文件. 上传文件普遍的过程: 一个上传文件用的表单,允许用户选择一个文件并上传它.当这个表单被提交,该文件被上传到指定的目录.同时,该文件将被验证是否符合您设定的要求.一旦文件上传成功,还要返回一个上传成功的确认窗口. 下面是表单: 复制代码 代码如下: <form method="post" action="<?=base_url()?>admin/img_uplo

  • CI框架实现优化文件上传及多文件上传的方法

    本文实例分析了CI框架实现优化文件上传及多文件上传的方法.分享给大家供大家参考,具体如下: 最近一直在研究Codeigniter框架,开发项目写到文件上传的时候发现大部分程序员使用Codeigniter框架的文件上传类编写上传方法的时候写的都存在这代码冗余(或者说代码重复利用率低.比较消耗资源.)故而我研究出一个稍微优化一点的上传方法.并且在查找资料时发现,Codeigniter框架同时上传多个文件比较困难,所以在优化方法的同时我又研究了一下如何使用Codeigniter框架实现同时上传多个文件

  • CI框架封装的常用图像处理方法(缩略图,水印,旋转,上传等)

    本文实例讲述了CI框架封装的常用图像处理方法.分享给大家供大家参考,具体如下: 其实微信手机端上图时,列表图最好是缩略图,节省流量,这不,又被移动坑了一把,话费签一分就停机,流量欠到90块才停机,我也是醉了... 不说废话了,下面是用CI 的内置处理图像的库写的,小弟不才,遗漏之处敬请指出,谢谢. /** * 生成缩略图 * @param $path 原图的本地路径 * @return null 创建一个 原图_thumb.扩展名 的文件 * */ public function dealthu

  • CI(CodeIgniter)框架实现图片上传的方法

    本文实例讲述了CodeIgniter框架实现图片上传的方法.分享给大家供大家参考,具体如下: 对于图片上传这种老生常谈的问题,在此我不得不再次重复一次,因为对于这框架毕竟有些地方值得自己学习与借鉴,这篇文章我是借助官方文档来写的,但有些地方任然需要标明一下. 下面我们来看看图片上传吧.首先在"./application/views/"文件夹下创一个视图文件:text.php,代码如下: <html> <head> <title>Upload Form

  • CodeIgniter上传图片成功的全部过程分享

    最近几天正在做一个小型CMS,用到图片上传了,想利于CodeIgniter的上传类去实现,但测试中有好多问题,我把经过和要注意的地方分享一下! 复制代码 代码如下: <?php echo form_open_multipart('picture/upload');?><?php echo form_upload('userfile');?> /*注意,这里是userfile,$this->upload->do_upload(),这里do_upload默认上传文件的表单名

  • php基于CodeIgniter实现图片上传、剪切功能

    本文实例为大家分享了codeigniter 图片上传.剪切,控制器类,供大家参考,具体内容如下 <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Index extends MY_Controller { function __construct(){ parent::__construct(); $this->load->helper(array('form', 'url')); }

  • CI框架文件上传类及图像处理类用法分析

    本文实例讲述了CI框架文件上传类及图像处理类用法.分享给大家供大家参考,具体如下: //列表页banner图片 public function edit_list_page_banner($category_id=""){ $category_id= empty($category_id)?$_POST["category_id"]:$category_id; //上传图片 if(isset($_POST["key"]) && $

随机推荐