Search code examples
codeigniterfile-uploadcodeigniter-2

Codeigniter - form validation doesn't work for files


i need to set a input file as required into my Codeigniter Controller. This is my form_validation:

$this->form_validation->set_rules('copertina','Foto principale','required|xss_clean');

and this is the form:

<?php echo form_open_multipart('admin/canile/nuovo'); ?>
<li class="even">
    <label for="copertina">Foto principale <span>*</span></label>
    <div class="input"><input type="file" name="copertina" value="<?php echo set_value('copertina'); ?>" id="copertina" /></div>    
</li>
<?php echo form_close(); ?>

But after the submit the form say that the file is not set, so the required clausole fails...how can i fix it?


Solution

  • File upload data is not stored in the $_POST array, so cannot be validated using CodeIgniter's form_validation library. File uploads are available to PHP using the $_FILES array.

    It maybe possible to directly manipulate the $_POST array using data from the $_FILES array, before running form validation, but I haven't tested this. It's probably best to just check the upload library process for errors.

    In addition, it is not possible, for security reasons, to (re-)set the value on page reload.