First jump to the view by means of the controller
Public function file ()
{ $this->load->helper (' url '); $this->load->view (' file ');}
Create a form in the view to select and upload a file
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Document</title></Head><Body> <formAction= "<?php echo site_url (' index.php/user/upload ');?>"Method= "POST"enctype= "Multipart/form-data"> <inputtype= "File"name= "Pic" /> <inputtype= "Submit"name= "Submit"value= "Submit"> </form></Body></HTML>
Note that the Name property of the first input, which is used after this property, is set to a controller method in the form, and the corresponding controller method is written
Public functionupload () {//uploading files to the server directory $config[' upload_path '] = './upload '; //What types are allowed to upload $config[' allowed_types '] = ' gif|png|jpg|jpeg '; //file name after upload, with uniqid () to ensure the file name is unique $config[' file_name '] =uniqid(); //loading the Upload library $this->load->library (' Upload ',$config); //upload the file, where pic is the Name property of the document control in the view $result=$this->upload->do_upload (' pic '); //If the upload is successful, get the information to upload the file if($result) { Var_dump($this->upload->data ()); }}
This completes the file upload.
CodeIgniter Study Notes (14) file upload in--ci