Home  >  Article  >  Backend Development  >  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

不言
不言Original
2018-06-14 14:30:191976browse

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.

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

load->model('brand_model');
    $this->load->library('form_validation');
    //激活分析器以调试程序
    $this->output->enable_profiler(TRUE);
    //配置中上传的相关参数会自动加载
    $this->load->library('upload');
  }
}

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

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('upload');
  //配置上传参数
  $upload_config = array(
    'upload_path' => './public/uploads/',
    'allowed_types' => 'jpg|png|gif',
    'max_size' => '500',
    'max_width' => '1024',
    'max_height' => '768',
  );
  $this->upload->initialize($upload_config);
  //循环处理上传文件
  foreach ($_FILES as $key => $value) {
    if (!empty($key['name'])) {
      if ($this->upload->do_upload($key)) {
        //上传成功
        print_r($this->upload->data());
      } else {
        //上传失败
        echo $this->upload->display_errors();
      }
    }
  }
}

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

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

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!

Statement:
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