Home > Backend Development > PHP Tutorial > How to use the CI framework to optimize file uploads and upload multiple files

How to use the CI framework to optimize file uploads and upload multiple files

不言
Release: 2023-04-01 07:50:02
Original
2110 people have browsed it

This article mainly introduces the method of CI framework to optimize file upload and multi-file upload. It analyzes in detail the implementation ideas and specific operation steps of CI framework to optimize file upload and multi-file upload in the form of examples. Friends in need can refer to it. Next

The example of this article analyzes the CI framework's method of optimizing file uploads and multiple file uploads. Share it with everyone for your reference, the details are as follows:

I have been studying the Codeigniter framework recently. When writing the file upload in the development project, I found that most programmers use the file upload class of the Codeigniter framework to write the upload method. This code redundancy exists (or the code reuse rate is low and consumes resources.) Therefore, I developed a slightly optimized upload method. And when searching for information, I found that it is difficult to upload multiple files at the same time with the Codeigniter framework, so while optimizing the method, I also studied how to use the Codeigniter framework to upload multiple files at the same time. Let me share it with you. Interested students can pay attention to it. You are welcome to correct any mistakes.

1. Optimize file upload method

The commonly used methods in the Codeigniter manual will not be described again here. Let’s directly talk about how to optimize the method. In order to achieve the purpose of reducing code redundancy and improving code reuse rate.

a) First create a new "upload.php" configuration file in "application/config"

Create a new "upload.php" configuration file in "application/config" and write the uploaded information in it Configuration parameters.

<?php
  defined(&#39;BASEPATH&#39;) OR exit(&#39;No direct script access allowed&#39;);
  //上传的参数配置
  $config[&#39;upload_path&#39;] = &#39;./public/uploads/&#39;;
  $config[&#39;allowed_types&#39;] = &#39;gif|png|jpg&#39;;
  $config[&#39;max_size&#39;] = 100;
  $config[&#39;max_width&#39;] = &#39;1024&#39;;
  $config[&#39;max_height&#39;] = &#39;768&#39;;
Copy after login

Note: The path folder represented by the upload_path parameter has been created in the project!

b) Load the file upload class in the constructor of the controller

<?php
defined(&#39;BASEPATH&#39;) OR exit(&#39;No direct script access allowed&#39;);
/**
 * 控制器
 */
class Brand extends Admin_Controller
{
  public function __construct()
  {
    parent::__construct();
    $this->load->model(&#39;brand_model&#39;);
    $this->load->library(&#39;form_validation&#39;);
    //激活分析器以调试程序
    $this->output->enable_profiler(TRUE);
    //配置中上传的相关参数会自动加载
    $this->load->library(&#39;upload&#39;);
  }
}
Copy after login

Note: The "upload. php” file will be automatically loaded here.

c) Write the upload method and execute the do_upload() method to upload the file

public function insert()
{
  //设置验证规则
  $this->form_validation->set_rules(&#39;brand_name&#39;,&#39;名称&#39;,&#39;required&#39;);
  if($this->form_validation->run() == false){
    //未通过验证
    $data[&#39;message&#39;] = validation_errors();
    $data[&#39;wait&#39;] = 3;
    $data[&#39;url&#39;] = site_url(&#39;admin/brand/add&#39;);
    $this->load->view(&#39;message.html&#39;,$data);
  }else{
    //通过验证,处理图片上传
    if ($this->upload->do_upload(&#39;logo&#39;)) { //logo为前端file控件名
      //上传成功,获取文件名
      $fileInfo = $this->upload->data();
      $data[&#39;logo&#39;] = $fileInfo[&#39;file_name&#39;];
      //获取表单提交数据
      $data[&#39;brand_name&#39;] = $this->input->post(&#39;brand_name&#39;);
      $data[&#39;url&#39;] = $this->input->post(&#39;url&#39;);
      $data[&#39;brand_desc&#39;] = $this->input->post(&#39;brand_desc&#39;);
      $data[&#39;sort_order&#39;] = $this->input->post(&#39;sort_order&#39;);
      $data[&#39;is_show&#39;] = $this->input->post(&#39;is_show&#39;);
      //调用模型完成添加动作
      if($this->brand_model->add_brand($data)){
        $data[&#39;message&#39;] = "添加成功";
        $data[&#39;wait&#39;] = 3;
        $data[&#39;url&#39;] = site_url(&#39;admin/brand/index&#39;);
        $this->load->view(&#39;message.html&#39;,$data);
      }else{
        $data[&#39;message&#39;] = "添加失败";
        $data[&#39;wait&#39;] = 3;
        $data[&#39;url&#39;] = site_url(&#39;admin/brand/add&#39;);
        $this->load->view(&#39;message.html&#39;,$data);
      }
    }else{
      //上传失败
      $data[&#39;message&#39;] = $this->upload->display_errors();
      $data[&#39;wait&#39;] = 3;
      $data[&#39;url&#39;] = site_url(&#39;admin/brand/add&#39;);
      $this->load->view(&#39;message.html&#39;,$data);
    }
  }
}
Copy after login

Note: Part of the above code is the code in my project , you can ignore and focus directly on the key upload code. When you need to upload different files, you can also configure the file upload in the method, using the $this->upload->initialize() method.

2. Two methods of uploading multiple files at the same time

① Method 1: Loop processing of multiple uploaded files

/**
 * Codeigniter框架实现多文件上传
 * @author Zhihua_W
 * 方法一:对上传的文件进行循环处理
 */
public function multiple_uploads1()
{
  //载入所需文件上传类库
  $this->load->library(&#39;upload&#39;);
  //配置上传参数
  $upload_config = array(
    &#39;upload_path&#39; => &#39;./public/uploads/&#39;,
    &#39;allowed_types&#39; => &#39;jpg|png|gif&#39;,
    &#39;max_size&#39; => &#39;500&#39;,
    &#39;max_width&#39; => &#39;1024&#39;,
    &#39;max_height&#39; => &#39;768&#39;,
  );
  $this->upload->initialize($upload_config);
  //循环处理上传文件
  foreach ($_FILES as $key => $value) {
    if (!empty($key[&#39;name&#39;])) {
      if ($this->upload->do_upload($key)) {
        //上传成功
        print_r($this->upload->data());
      } else {
        //上传失败
        echo $this->upload->display_errors();
      }
    }
  }
}
Copy after login

② Method 2: Upload multiple files directly and then process the uploaded data

/**
 * Codeigniter框架实现多文件上传
 * @author Zhihua_W
 * 方法二:直接一下将多个文件全部上传然后在对上传过的数据进行处理
 */
public function multiple_uploads2()
{
  $config[&#39;upload_path&#39;] = &#39;./public/uploads/&#39;;
  //这里的public是相对于index.php的,也就是入口文件,这个千万不能弄错!
  //否则就会报错:"The upload path does not appear to be valid.";
  $config[&#39;allowed_types&#39;] = &#39;gif|jpg|png&#39;;
  //我试着去上传其它类型的文件,这里一定要注意顺序!
  //否则报错:"A problem was encountered while attempting to move the uploaded file to the final destination."
  //这个错误一般是上传文件的文件名不能是中文名,这个很郁闷!还未解决,大家可以用其它方法,重新改一下文件名就可以解决了!
  //$config[&#39;allowed_types&#39;] = &#39;zip|gz|png|gif|jpg&#39;;(正确)
  //$config[&#39;allowed_types&#39;] = &#39;png|gif|jpg|zip|gz&#39;;(错误)
  $config[&#39;max_size&#39;] = &#39;1024&#39;;
  $config[&#39;max_width&#39;] = &#39;1024&#39;;
  $config[&#39;max_height&#39;] = &#39;768&#39;;
  $config[&#39;file_name&#39;] = time(); //文件名不使用原始名
  $this->load->library(&#39;upload&#39;, $config);
  if (!$this->upload->do_upload()) {
    echo $this->upload->display_errors();
  } else {
    $data[&#39;upload_data&#39;] = $this->upload->data(); //上传文件的一些信息
    $img = $data[&#39;upload_data&#39;][&#39;file_name&#39;]; //取得文件名
    echo $img . "<br>";
    foreach ($data[&#39;upload_data&#39;] as $item => $value) {
      echo $item . ":" . $value . "<br>";
    }
  }
}
Copy after login

Which of the two methods is more convenient? Which one is more efficient? You can try it yourself!

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About analysis of CodeIgniter framework verification code library files and usage

About commonly used CI framework encapsulation Image processing method

How to use the CodeIgniter framework to implement image uploading method

The above is the detailed content of How to use the CI framework to optimize file uploads and upload multiple files. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template