CodeIgniter file Upload class allows files to be uploaded. You can set a file that specifies to upload a file of a certain type and a specified size.
Common process for uploading files:
- A form for uploading files that allows the user to 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 the requirements you set.
- Once the file is uploaded successfully, return a confirmation window with the successful upload.
Here is the form:
Then the following 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 '] = ' $ '; $config [' max_width '] = ' 1024x768 '; $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 ())} }
Preferences setting parameters
Preference Settings |
Default Value |
Options |
Description |
Upload_path |
None |
None |
File upload path. The path must be writable, and both the relative and absolute paths can be. |
Allowed_types |
None |
None |
The MIME type of the file that is allowed to be uploaded, usually the file extension can be MIME type. Allow multiple types with a vertical bar ' | ' Separate |
file_name |
None |
The file name you want to use |
If this parameter is set, CodeIgniter will rename the uploaded file according to the file name set here. The extension in the file name must also be the allowed file type. |
Overwrite |
FALSE |
True/false (Boolean) |
is overwritten. When this parameter is true, the original file will be overwritten if the file is uploaded with the same name, and if the parameter is False,ci, a number will be appended to the filename of the new file. If set to True, if a file with the same name as the one is uploading exists, it'll be overwritten. If set to False, a number would be appended to the filename if another with the same name exists. |
Max_size |
0 |
None |
The maximum number of file sizes allowed to upload (in K). This parameter is 0 without restriction. Note: PHP usually has this limitation, which can be specified in the php.ini file. Typically, the default is 2MB. |
Max_width |
0 |
None |
The maximum width (in pixels) for the uploaded file. 0 is not limited. |
Max_height |
0 |
None |
The maximum height (in pixels) of the uploaded file. 0 is not limited. |
Max_filename |
0 |
None |
The maximum length of the file name. 0 is not limited. |
Encrypt_name |
FALSE |
True/false (Boolean) |
Whether to rename the file. If this argument is true, the uploaded file will be renamed to a random encrypted string. This is useful when you want to make it impossible for a file uploader to differentiate the file names of the files that they upload. This option only works when overwrite is FALSE. |
Remove_spaces |
TRUE |
True/false (Boolean) |
When the argument is true, the space in the file name is replaced with an underscore. Recommended use. |
A few of the functions used
- $this->upload->do_upload (): Perform the action based on your preference configuration parameters. Note: Files uploaded by default come from a file field named UserFile in the submission form, and the form must be of type "multipart".
- $this->upload->display_errors (): If do_upload () returns failure, an error message is displayed. This function does not automatically output, but returns data, so you can arrange it as you want.
- $this->upload->data (): This is an auxiliary function that returns an array of all relevant information about the file you uploaded.
http://www.bkjia.com/PHPjc/752371.html www.bkjia.com true http://www.bkjia.com/PHPjc/752371.html techarticle CodeIgniter File Upload class allows files to be uploaded. You can set a file that specifies to upload a file of a certain type and a specified size. Upload files Common process: An upload file with the ...