Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeIgniter - File upload required validation
    text
    copied!<p>I have 2 text fields and 1 file upload that are all required. Everything works when I require just the text fields, but when I require the file upload the validation error stays saying that a file is required, even though I selected one. Anyone know what I am doing wrong? Many thanks in advance.</p> <p>//view</p> <pre><code>&lt;?php echo form_open_multipart('add'); ?&gt; &lt;fieldset&gt; &lt;input type="text" name="name" /&gt;&lt;br&gt; &lt;input type="text" name="code" /&gt;&lt;br&gt; &lt;input type="file" name="userfile" /&gt;&lt;br&gt;&lt;br&gt; &lt;input type="submit"value="Add" /&gt; &lt;/fieldset&gt; &lt;?php echo form_close(); ?&gt; </code></pre> <p>//controller</p> <pre><code>public function add() { $this-&gt;form_validation-&gt;set_rules('name', 'Name', 'required'); $this-&gt;form_validation-&gt;set_rules('code', 'Code', 'required'); //$this-&gt;form_validation-&gt;set_rules('userfile', 'Document', 'required'); //when the above line is active the upload does not go through if ($this-&gt;form_validation-&gt;run() == FALSE) { $data['page_view'] = $this-&gt;page_view; $data['page_title'] = $this-&gt;page_title; $this-&gt;load-&gt;view('template', $data); } else { $this-&gt;load-&gt;library('upload'); if (!empty($_FILES['userfile']['name'])) { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png|jpeg'; $this-&gt;upload-&gt;initialize($config); if ($this-&gt;upload-&gt;do_upload('userfile')) { $img = $this-&gt;upload-&gt;data(); $file_name = $img['file_name']; $name = $this-&gt;input-&gt;post('name'); $code = $this-&gt;input-&gt;post('code'); $this-&gt;load-&gt;model('create', 'create_model'); $this-&gt;create_model-&gt;create_entry($name, $code, $file_name); $data['page_view'] = $this-&gt;page_view; $data['page_title'] = $this-&gt;page_title; $this-&gt;load-&gt;view('template', $data); } else { echo $this-&gt;upload-&gt;display_errors(); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload