The CI framework optimizes file upload and multi-File Upload methods, and the ci framework optimizes file upload.

Source: Internet
Author: User
Tags codeigniter

The CI framework optimizes file upload and multi-File Upload methods, and the ci framework optimizes file upload.

This article provides an example of how to optimize file upload and multi-File Upload using the CI framework. We will share this with you for your reference. The details are as follows:

I have been studying the Codeigniter framework recently, when writing to a file for upload in a development project, it is found that most programmers use the File Upload class of the Codeigniter framework to write the upload method. This code is redundant (or the code reuse rate is low, which consumes resources.) Therefore, I have developed a slightly optimized upload method. In addition, it was found that it was difficult for the Codeigniter framework to upload multiple files at the same time when searching for information. So while optimizing the method, I also studied how to use the Codeigniter framework to upload multiple files at the same time. I would like to share with you the following. If you are interested, please pay attention to it. You are also welcome to correct the mistakes.

1. Optimized File Upload Methods

The common methods in the Codeigniter manual are not described here. The following describes how to optimize the methods to reduce code redundancy and improve code reuse.

A) first create the "upload. php" configuration file in "application/config ".

Create the "upload. php" configuration file in "application/config" and write the uploaded configuration parameters.

<? Php defined ('basepath') OR exit ('no direct script access allowed'); // The parameter configuration for uploading $ config ['upload _ path'] = '. /public/uploads/'; $ config ['allowed _ types'] = 'gif | png | jpg '; $ config ['max _ size'] = 100; $ config ['max _ width'] = '000000'; $ config ['max _ height'] = '20140901 ';

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

B) load the File Upload class in the Controller 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 '); // activate the analyzer to debug the program $ this-> output-> enable_profiler (TRUE ); // The parameters uploaded in the configuration will automatically load $ this-> load-> library ('upload ');}}

Note: The upload configuration information in the "upload. php" File Created in step 1 will be automatically loaded here.

C) Compile the upload method and execute the do_upload () method to upload files.

Public function insert () {// set verification rules $ this-> form_validation-> set_rules ('brand _ name', 'name', 'required '); if ($ this-> form_validation-> run () = false) {// Verification Failed $ data ['message'] = validation_errors (); $ data ['wait'] = 3; $ data ['url'] = site_url ('admin/brand/add'); $ this-> load-> view('message.html ', $ data);} else {// after verification, process the Image upload if ($ this-> upload-> do_upload ('logo ')) {// The logo indicates the front-end file control name. // the file name is successfully uploaded. Obtain the file name $ fileInfo = $ this-> upload-> data (); $ data ['logo '] = $ fileInfo ['file _ name']; // obtain the data submitted by the form $ 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'] = "added successfully "; $ data ['wait'] = 3; $ data ['url'] = site_url ('admin/brand/Index'); $ this-> load-> view('message.html ', $ data);} else {$ data ['message'] = "failed to add"; $ 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 the code in my project. You can ignore the key upload code directly. When you need to upload different files, you can configure the file upload in the method by using the $ this-> upload-> initialize () method.

2. upload multiple files simultaneously

① Method 1: Process uploaded multiple files cyclically

/*** Implement Multifile upload using the Codeigniter framework * @ author zhi1__w * Method 1: cyclically process uploaded files */public function multiple_uploads1 () {// load the required file upload class library $ this-> load-> library ('upload '); // configure the upload parameter $ upload_config = array ('upload _ path' => '. /public/uploads/', 'allowed _ types' => 'jpg | png | gif', 'max _ size' => '123 ', 'max _ width' => '200', 'max _ height' => '200',); $ this-> upload-> initialize ($ upload_config ); // process uploaded FILES cyclically ($ _ FILES as $ ke Y => $ value) {if (! Empty ($ key ['name']) {if ($ this-> upload-> do_upload ($ key )) {// print_r ($ this-> upload-> data ());} else {// upload Failed echo $ this-> upload-> display_errors ();}}}}

② Method 2: directly upload all the files and process the uploaded data.

/*** Implement Multifile upload using the Codeigniter framework * @ author zhi1__w * Method 2: directly upload all the files and process the uploaded data */public function multiple_uploads2 () {$ config ['upload _ path'] = '. /public/uploads/'; // here the public is relative to the index. php, that is, the entry file, must be correct! // Otherwise, an error is returned: "The upload path does not appear to be valid. "; $ config ['allowed _ types'] = 'gif | jpg | png '; // I tried to upload other types of files. Pay attention to the sequence here! // Otherwise, an error is returned: "A problem was encountered while attempting to move the uploaded file to the final destination. "// This error is generally because the file name to be uploaded cannot be a Chinese name. This is depressing! Not solved yet. You can use other methods to solve the problem by modifying the file name! // $ Config ['allowed _ types '] = 'zip | gz | png | gif | jpg'; (correct) // $ config ['allowed _ types '] = 'png | gif | jpg | zip | gz'; (error) $ config ['max _ size'] = '000000'; $ config ['max _ width'] = '20140901 '; $ config ['max _ height'] = '000000'; $ config ['file _ name'] = time (); // do 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 file Information $ img = $ data ['upload _ data'] ['file _ name']; // obtain the file name echo $ img. "<br>"; foreach ($ data ['upload _ data'] as $ item => $ value) {echo $ item. ":". $ value. "<br> ";}}}

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

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.