CodeIgniter: The entire process of successfully uploading an image _ PHP Tutorial

Source: Internet
Author: User
Tags codeigniter
CodeIgniter shares the entire process of successfully uploading an image. I have been working on a small CMS in recent days and used image upload. I want to facilitate CodeIgniter's upload class implementation. but there are many problems in the test. I will share my experience with you! Copying a small CMS that has been used for image upload in recent days is intended to facilitate CodeIgniter's upload class implementation. However, there are many problems in the test. I will share the article with you!

The code is as follows:




/* Note: Here is userfile, $ this-> upload-> do_upload (). here, the form name of the file uploaded by do_upload is userfile by default;
You can also use do_upload ($ filename). $ filename must be consistent with the string in form_upload.
I am confused here. I can only understand it after reading the manual. I hope you will pay attention to it!
*/

Controller code:
Function upload (){
$ Config ['upload _ path'] = './uploads /';
/* Here, uploads is relative to index. php, that is, the entry file. do not make a mistake!
Otherwise, The error "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!
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'] = '000000 ';
$ Config ['max _ height'] = '20140901 ';

$ 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 (); // File information
$ Img = $ data ['upload _ data'] ['File _ name']; // get the file name

Echo $ img ."
";

Foreach ($ data ['upload _ data'] as $ item => $ value ){
Echo $ item. ":". $ value ."
";

}

}
}

Success! Copy...

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.