Example of using the CodeIgniter class library to upload images
2. file Upload class:
-
Public function img_upload ()
- {
- $ This-> load-> helper ('URL ');
-
$ Config ['upload _ path'] = './images/'. date ('ym', time ()).'/';
- $ Config ['allowed _ types'] = 'gif | jpg | png ';
- $ Config ['File _ name'] = date ('Y _ m_d ', time ().' _ '. sprintf (' % 02d ', rand (0, 99 ));
- $ Config ['max _ size'] = '000000 ';
- $ Config ['max _ width'] = '000000 ';
- $ Config ['max _ height'] = '20140901 ';
-
$ This-> load-> library ('upload', $ config );
-
If (! $ This-> upload-> do_upload ())
- {
- $ Error = array ('error' => $ this-> upload-> display_errors ());
- }
- Else
- {
- $ Data = array ('upload _ data' => $ this-> upload-> data ());
- }
- }
Required functions: $ this-> upload-> do_upload (): configure parameters according to preference to perform the operation. Note: By default, the uploaded file is from the file field named userfile in the submitted form, and the form must be of the "multipart" type. $ This-> upload-> display_errors (): If do_upload () fails to return, an error message is displayed. This function does not automatically output but returns data, so you can arrange it as required. $ This-> upload-> data (): Helper function, which returns an array of all relevant information of the uploaded file. |