Code example for CodeIgniter to upload images
Share a piece of code for CodeIgniter to upload images. for a friend studying the php framework of CodeIgniter, you can make a reference. I used the CodeIgniter Upload class to upload images. I encountered some problems during the test. Here are some notes:
/* Note: Here is userfile, $ this-> upload-> do_upload (). here, do_upload uploads the file's form name userfile by default. of course, you can also use do_upload ($ filename ), $ filename must be consistent with the string in form_upload. */
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'] = '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 (); // file information $ img = $ data ['upload _ data'] ['File _ name']; // Obtain the file name echo $ img." "; Foreach ($ data ['upload _ data'] as $ item => $ value) {echo $ item.": ". $ value ." ";}}} |