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...