How to use the CI framework for File upload optimization and multi-file upload

Source: Internet
Author: User
Tags codeigniter
This article mainly introduced the CI framework to achieve the optimization of file upload and multi-file upload method, combined with the case of a detailed analysis of the CI framework optimization file upload and the implementation of multi-file upload ideas and specific operating procedures, the need for friends can refer to the next

This paper analyzes the method of the CI framework to realize the optimized file uploading and multi-file uploading. Share to everyone for your reference, as follows:

Recently has been studying the CodeIgniter framework, the development project written to the file upload when the majority of programmers use the CodeIgniter framework of the file upload class to write the upload method is written when the code is redundant (or code reuse low, compared to consume resources. So I developed a slightly optimized upload method. And when looking for data, found that the CodeIgniter framework to upload multiple files at the same time is difficult, so in the optimization method, I also studied how to use the CodeIgniter framework to upload multiple files simultaneously. Here to share with you, interested students can pay attention to, at the same time welcome you to correct mistakes.

1. Optimized file Upload method

CodeIgniter manual The kind of commonly used methods here do not repeat the description, the following direct how to optimize the method to achieve reduced code redundancy, improve code reuse purposes.

A) first create a new "upload.php" configuration file in "Application/config"

In the "Application/config" new "upload.php" configuration file, write the upload configuration parameters.

<?php  defined (' BasePath ') or exit (' No Direct script access allowed ');  The uploaded parameters are configured  $config [' upload_path '] = './public/uploads/';  $config [' allowed_types '] = ' gif|png|jpg ';  $config [' max_size '] = +;  $config [' max_width '] = ' 1024x768 ';  $config [' max_height '] = ' 768 ';

Note: The path folder represented by the Upload_path parameter you have created in the project!

b) Load the file upload class in the controller's constructor

<?phpdefined (' BasePath ') or exit (' No Direct script access allowed ');/** * Controller */class Brand extends admin_controller{
  public function __construct ()  {    parent::__construct ();    $this->load->model (' Brand_model ');    $this->load->library (' form_validation ');    Activates the parser to debug the program    $this->output->enable_profiler (TRUE);    The relevant parameters uploaded in the configuration will be loaded automatically    $this->load->library (' upload ');}  }

Note: The upload configuration information in the "upload.php" file that we created in the first step is automatically loaded here.

c) Write upload method Execution Do_upload () method for file upload

Public Function Insert () {//set validation rule $this->form_validation->set_rules (' brand_name ', ' name ', ' required ');    if ($this->form_validation->run () = = False) {//failed validation $data [' message '] = Validation_errors ();    $data [' wait '] = 3;    $data [' url '] = Site_url (' Admin/brand/add ');  $this->load->view (' message.html ', $data);  }else{//through verification, processing image upload if ($this->upload->do_upload (' logo ')) {//logo for the front-end file control name//upload succeeded, get the file name $fileInfo      = $this->upload->data ();      $data [' logo '] = $fileInfo [' file_name '];      Get form submission Data $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 ');        Call the model to complete the Add action if ($this->brand_model->add_brand ($data)) {$data [' message '] = "add success";       $data [' wait '] = 3; $data [' url '] = Site_url (' Admin/brand/index ');      $this->load->view (' message.html ', $data);        }else{$data [' message '] = "Add Failed";        $data [' wait '] = 3;        $data [' url '] = Site_url (' Admin/brand/add ');      $this->load->view (' message.html ', $data);      }}else{//upload failed $data [' message '] = $this->upload->display_errors ();      $data [' wait '] = 3;      $data [' url '] = Site_url (' Admin/brand/add ');    $this->load->view (' message.html ', $data); }  }}

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

2, simultaneous uploading of multiple files of two ways

① method One idea: The upload of multiple files to the loop processing

/** * CodeIgniter framework for multi-file upload * @author Zhihua_w * Method One: The uploading of files to cycle processing */public function multiple_uploads1 () {  //load the required file Upload class Library  $this->load->library (' upload ');  Configure the upload parameters  $upload _config = Array (    ' upload_path ' = = './public/uploads/',    ' allowed_types ' = ' jpg|png ' |gif ',    ' max_size ' = ' + ',    ' max_width ' = ' 1024x768 ',    ' max_height ' = ' 768 ', '  ;  $this->upload->initialize ($upload _config);  Loop process upload file  foreach ($_files as $key = = $value) {    if (!empty ($key [' name '])) {      if ($this->upload- >do_upload ($key)) {        //upload successful        print_r ($this->upload->data ());      } else {        //upload failed        echo $this->upload->display_errors ();}}}  

② method Two ideas: directly upload multiple files and then process the uploaded data.

/** * CodeIgniter framework for multi-file uploads * @author Zhihua_w * Method Two: Directly upload multiple files and then process the uploaded data */public function Multiple_uploads2 () {$c  onfig[' upload_path '] = './public/uploads/';  The public here is relative to the index.php, that is, the entry file, this must not be mistaken!  Otherwise you will get an error: "The upload path does not appear to be valid.";  $config [' allowed_types '] = ' gif|jpg|png ';  I try to upload other types of files, here must pay attention to the order!  Otherwise error: "A problem is encountered while attempting to move the uploaded file to the final destination."  This error is generally uploaded file filename can not be Chinese name, this is very depressed! Not resolved, you can use other methods, re-change the file name can be solved! $config [' allowed_types '] = ' zip|gz|png|gif|jpg ';(correct)//$config [' allowed_types '] = ' png|gif|jpg|zip|gz ';(error) $  config[' max_size '] = ' 1024 ';  $config [' max_width '] = ' 1024 ';  $config [' max_height '] = ' 768 '; $config [' file_name '] = time ();  The file name does not use the original name $this->load->library (' upload ', $config);  if (! $this->upload->do_upload ()) {echo $this->upload->display_errors (); } else {$data [' upload_data '] = $this->upload->data ();////Upload some information about the file $img = $data [' Upload_data' [' file_name ']; Gets the file name Echo $img.    "<br>"; foreach ($data [' Upload_data '] as $item = + $value) {echo $item. ":" . $value.    "<br>"; }  }}

Two methods, which is more convenient? That's more efficient? You can try it on your own!

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

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.