CodeIgniter's file upload class allows files to be uploaded. You can specify the file type to be uploaded and the file size to be specified.
The general process of uploading files:
- A form used to upload a file. You can select a file and upload it.
- When this form is submitted, the file is uploaded to the specified directory.
- At the same time, the file will be verified to meet your set requirements.
- Once the file is uploaded successfully, a confirmation window is returned.
The following is a form:
<Form method = "post" action = "<? = Base_url ()?> Admin/img_upload/"enctype =" multipart/form-data "/> <div style =" margin: 0 0 0.5em 0em; "> <input type =" file "name =" userfile "size =" 20 "class =" button "/> <input type =" submit "value =" Upload "class = "button"/> </div> </form>
Next is the 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'] = '500';$config['max_width'] = '1024';$config['max_height'] = '768';$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()); }}Preference Parameters
| Preference settings |
Default Value |
Option |
Description |
| Upload_path |
None |
None |
File Upload path. The path must be writable. The relative path and absolute path can both be used. |
| Allowed_types |
None |
None |
The MIME type of the file to be uploaded. Generally, the file extension can be used as the MIME type. Multiple types can be separated by vertical bars (| ). |
| File_name |
None |
File name to be used |
If this parameter is set, CodeIgniter will rename the uploaded file based on the file name set here. The file name extension must also be of the allowed file type. |
| Overwrite |
FALSE |
TRUE/FALSE (boolean) |
Overwrite or not. If this parameter is set to TRUE, the original file will be overwritten if the file is uploaded with a duplicate name. If this parameter is set to FALSE, CI will add a number after the file name of the new file. If set to true, if a file with the same name as the one you are uploading exists, it will be overwritten. if set to false, a number will be appended to the filename if another with the same name exists. |
| Max_size |
0 |
None |
Maximum size of the file to be uploaded (in K ). If this parameter is set to 0, no restriction is imposed. Note: PHP also has this restriction, which can be specified in the php. ini file. The default value is 2 MB. |
| Max_width |
0 |
None |
The maximum width (in pixels) of the uploaded file ). 0 is unlimited. |
| Max_height |
0 |
None |
The maximum height (in pixels) of the uploaded file ). 0 is unlimited. |
| Max_filename |
0 |
None |
The maximum length of a file name. 0 is unlimited. |
| Encrypt_name |
FALSE |
TRUE/FALSE (boolean) |
Whether to rename the file. If this parameter is set to TRUE, the uploaded file is renamed as a random encrypted string. It is useful when you want the File Uploader not to distinguish the file name uploaded by himself. This option takes effect only when overwrite is FALSE. |
| Remove_spaces |
TRUE |
TRUE/FALSE (boolean) |
If the parameter is TRUE, the space in the file name is replaced with an underscore. Recommended. |
Several functions used
- $ This-> upload-> do_upload (): Configure parameters according to your preference to perform the operation. Note: by default, the uploaded file is namedUserfileAnd the form must be of the "multipart" type.
- $ This-> upload-> display_errors (): IfDo_upload ()An error is returned. This function does not automatically output but returns data, so you can arrange it as required.
- $ This-> upload-> data (): this is an auxiliary function that returns an array of all relevant information about the uploaded file.