Codeigniter Upload File not working
Solution 1
The first thing you have to put the name of the file field.
<input type="file" name="image"/>
$this->upload->do_upload('image');
Second, you can not have the max_width and max height 0
$config['max_width'] = '2048';
$config['max_height'] = '2048';
Try that first and then see
For validate field file:
if($_FILES['you_field_name']['tmp_name']){
//your code
}
A greeting
Solution 2
Try this one check if $_FILES
is not empty then do the validation else do nothing
$my_rules = array(
array(
'field' => 'title',
'label' => 'Title',
'rules' => 'required|min_length[5]|max_length[20]|xss_clean'
)
);
if(!empty($_FILES)){
$my_rules[]= array(
'field' => 'additionalUpload',
'label' => 'Additional Upload',
'rules' => 'callback_is_image'
)
}
$this->form_validation->set_rules($my_rules);
In your upload function you need to specify the field name
$this->upload->do_upload('your_field_name')
Mohammed Sufian
A Hack Programmer, capable writing codes using almost all programming languages. He is also involved in maintaining & securing Linux/Windows Servers (Vulnerability / Penetration Tester) Of course, I can't survive without the Internet & I guess you too ;-) Quick Learning & Implementing Ability... Googling and implementing, optimizing, testing, and using best practices... You can connect with Sufian using mohammedsufianshaikh at gmail
Updated on June 04, 2022Comments
-
Mohammed Sufian almost 2 years
function submit_article() { $this->load->helper(array('form', 'url')); $this->load->library('form_validation'); $this->form_validation->set_error_delimiters('<p style="color:red">', '<br/></p>'); $my_rules = array( array( 'field' => 'title', 'label' => 'Title', 'rules' => 'required|min_length[5]|max_length[20]|xss_clean' ), array( 'field' => 'additionalUpload', 'label' => 'Additional Upload', 'rules' => 'callback_is_image' ) ); $this->form_validation->set_rules($my_rules); if ($this->form_validation->run() == FALSE) { //ERROR $data['title'] = ucfirst('submit Article'); $this->load->view('templates/header', $data); $this->load->view('submit_article', $data); $this->load->view('templates/footer', $data); } else { //SUCCESS $data['title'] = ucfirst('article Submitted'); $this->load->view('templates/header', $data); $this->load->view('forms_view/submit_article_success', $data); $this->load->view('templates/footer', $data); } } function is_image($value) { $config['upload_path'] = './public/uploads/'; $config['allowed_types'] = 'gif|jpg|png|pdf|tiff'; $config['max_size'] = '2048'; $config['max_width'] = '0'; $config['max_height'] = '0'; $config['remove_spaces'] = true; $this->load->library('upload', $config); if (!$this->upload->do_upload()) { $this->form_validation->set_message('is_image', $this->upload->display_errors('<p style="color:red">', '<br/></p>')); return FALSE; } else { $this->upload->data(); return TRUE; } }
Hi every one, this is my controller function code for processing multipart form data in codeigniter, actually the field additionalUpload is not a required feild, but I want it to be validated if the user upload file in additionalUpload field of file type, when I run the above code and click on submit button without selecting any file its shows me error "You did not select a file to upload." which I do not want because this is not a required field, this is my first problem..
and second one is that when I select a file and click on submit button it again show me that "You did not select a file to upload.".
Note: I have just shown two fields of my form here that is title,additionalUpload but I have all total 9 fields.
THANKS IN ADVANCE PLEASE HELP ANYONE.