CodeIgniter's file upload class allows files to be uploaded. You can set up to upload files of a certain type and size.
The common process for uploading files:
Here is the 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=" 上传 " class="button" /> </div> </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'] = '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()); } }
Preferences | Defaults | Options | Description | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
upload_path | None | None | File upload path. The path must be writable, both relative and absolute paths are acceptable. | ||||||||||||||||||||||||||||||||||||||||||||
allowed_types | None | None | MIME types that allow uploading files; usually file extensions can As a MIME type. Allow multiple types separated by vertical bars '|' | ||||||||||||||||||||||||||||||||||||||||||||
file_name | None | Want to use File name |
|
||||||||||||||||||||||||||||||||||||||||||||
overwrite | FALSE | TRUE/FALSE (boolean) | Whether to overwrite. When this parameter is TRUE, if a file with the same name is encountered when uploading a file, the original file will be overwritten; if this parameter is 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 | The maximum allowed upload file size (in K). If this parameter is 0, there is no limit. Note: Usually PHP also has this restriction, which can be specified in the php.ini file. Usually the default is 2MB. | ||||||||||||||||||||||||||||||||||||||||||||
max_width | 0 | None | The maximum width of the uploaded file (in pixels). 0 means no limit. | ||||||||||||||||||||||||||||||||||||||||||||
max_height | 0 | None | The maximum height of the uploaded file (in pixels). 0 means no limit. | ||||||||||||||||||||||||||||||||||||||||||||
max_filename | 0 | None | The maximum length of the file name. 0 means no limit. | ||||||||||||||||||||||||||||||||||||||||||||
encrypt_name | FALSE | TRUE/FALSE (boolean) | Whether to rename the file. If this parameter is TRUE, the uploaded file will be renamed to a random encrypted string. This is very useful when you want the file uploader to be unable to distinguish the file names of the files they upload. This option only works when overwrite is FALSE. | ||||||||||||||||||||||||||||||||||||||||||||
remove_spaces | TRUE | TRUE/FALSE (boolean) | When the parameter is TRUE, the file name Spaces in will be replaced with underscores. Recommended. |