Welcome Guest, Not a member yet? Register   Sign In
How can I validate a file upload using Form_validation?
#1

[eluser]KeyStroke[/eluser]
Hi,

I have a form where users can fill up and then attach a photo to it. I've attempted to validate the file upload field using this call back (which I've found on the forum):
Code:
function _do_upload($file)
{
    $config['upload_path'] = './folder/';
    $config['allowed_types'] = 'jpg|jpeg|png';
    $this->load->library('upload', $config);
        
    if ( ! $this->upload->do_upload())
    {
        $this->form_validation->set_message('_do_upload', $this->upload->display_errors());
        return FALSE;
    }    
    else
    {
        return TRUE;
    }
}

The problem with this solution is if the file is valid, but another field in the form is invalid, that file will be uploaded anyway!

What other way could I use to validate file uploads with the form_validation library?

Your help is much appreciated Smile
#2

[eluser]Mike Ryan[/eluser]
[quote author="KeyStroke" date="1241032243"]Hi,

What other way could I use to validate file uploads with the form_validation library?

[/quote]

Hi,

The short answer is... don't! :-)

IMO the validation callback should really just *check* to make sure it is a valid upload, not actually do it. The callback can check if the uploaded file is valid (e.g. accepted file extension, not too big etc) and then your controller can save the uploaded file if it passed all of the validation rules.
#3

[eluser]KeyStroke[/eluser]
Yea that's what I thought as well, but the thing is the file upload class doesn't have a method that simply allows me to "check" a file without uploading it, does it?
#4

[eluser]Mike Ryan[/eluser]
No, it doesn't. How about:

1) Validation callback does the upload, and stores the file in a temporary area
2) Uploaded file is checked according to your rules
3a) Validation failed: Delete temporary file, validation errors
3b) Validation passed: Your controller can move the temporary file to its intended locatation

Depending how large your files are you could also store them in between requests, so when another field fails to validate they do not need to re-upload them (perhaps by storing the temporary name in the user's session).
#5

[eluser]LuckyFella73[/eluser]
Maybe it's not nessesary to upload to a temp direction first.
If the whole validation process fails and you delete the uploaded
file anyway there is no big difference between deleting the file
from a temp directory or the "real" directory.

What I do in my callback function:
Code:
// config stuff here
if ( ! $this->upload->do_upload() )
{
    $this->form_validation->set_message('__do_upload_logo', $this->upload->display_errors());
    return FALSE;
}
else
{
    $filedata = $this->upload->data();
    $this->name_uploaded_file = $filedata['file_name'];
    return TRUE;
}

And in the code section where the validation failed:
Code:
if ($this->form_validation->run() == FALSE)
{
    if ( file_exists($this->upload_path.$this->name_uploaded_file) AND  strlen($this->name_uploaded_file)>1 )
    {
        unlink($this->upload_path.$this->name_uploaded_file);
    }
    $this->load->view(get_class($this).'/your_form.php', $data);
}
$this->upload_path is defined in my class constructor
#6

[eluser]KeyStroke[/eluser]
I just found a good solution that suits my needs. Assuming your validation code is the last bit of your controller's function, this will work:


Code:
if( $this->form_validation->run() == TRUE )
{
   // when all the fields are valid, validate the pic
   if( isset($_FILES) )
   {
      /* configure your upload here and then attempt to upload the file */

      if ( ! $this->upload->do_upload())
      {
           // if there are any errors, reload the form and send the upload error to it
       $view_data['upload_error'] = $this->upload->display_errors();
      
           $this->load->view('myform', $view_data);
           return;
      }
   }
    
}
// rest of the function's code

Then, in the form's View, you could simply do like what I do:
Code:
<?php if(validation_errors() || isset($error_upload)): ?>
<div class="errors">
<ul>
    &lt;?=validation_errors()?&gt;
    &lt;?php if(isset($upload_error)): ?&gt;
    <li>&lt;?=$upload_error?&gt;</li>
    &lt;?php endif; ?&gt;
</ul>
</div>
&lt;?php endif; ?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB