you use the codeigniter file loader class ... and call $this->upload->do_upload(); in the ahd conditional expression, check if it is true.
<?php if ( ! $this->upload->do_upload()){ $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_form', $error); } else{ $data = array('upload_data' => $this->upload->data()); $this->load->view('upload_success', $data); }
User_guide explains this in detail: http://codeigniter.com/user_guide/libraries/file_uploading.html
However, if you are dead, set to check if the file was โuploadedโ aka .. presented before you call this class (you donโt know why you did it). You can access PHPs $_FILES super global .. and use a conditional expression to check for size> 0.
http://www.php.net/manual/en/reserved.variables.files.php
Update 2: This is the actual working code, I myself use it on the avatar loader using CI 2.1
<?php //Just in case you decide to use multiple file uploads for some reason.. //if not, take the code within the foreach statement foreach($_FILES as $files => $filesValue){ if (!empty($filesValue['name'])){ if ( ! $this->upload->do_upload()){ $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_form', $error); }else{ $data = array('upload_data' => $this->upload->data()); $this->load->view('upload_success', $data); } }//nothing chosen, dont run. }//end foreach
source share