Welcome Guest, Not a member yet? Register   Sign In
file upload validation
#1

[eluser]badgeek[/eluser]
how to validate file upload using validation class?

thanks
#2

[eluser]Gordaen[/eluser]
What about the file are you trying to validate? Did you take a look at the File Uploading Class?
#3

[eluser]Isos[/eluser]
I have came to this issue .. I am using file uploading class with validation class. The form contains a lot of fields to be validated .. So, logically thinking, I created $rules['userfile'] = "callback__userfile";
and then a function:
Code:
function _userfile(){
        if (!$this->upload->do_upload('userfile'))
        {
            $this->validation->set_message('_userfile',$this->upload->display_errors());

            return false;
        } else {
            return true;
        }
    }

This is supposed to work! but even when I pass wrong extensions (not specified in $config['allowed_types']) or even when I don't pass any file (leave field empty) validation works without any errors, and when I select a file with the right conditions also validation works and no upload is done. I am sure of 777 permission, and everything else cuz I am using file upload in other places in my website.

Any tips? Was the idea of creating a callback a bad one?

Thanks
#4

[eluser]wiredesignz[/eluser]
Upload uses $_FILES and Validation uses $_POST so I can't see how you could validate this way. Sorry.

Maybe some hidden form fields might help, but I haven't investigated this.
#5

[eluser]xwero[/eluser]
Isos it's a great idea but you have no configuration. I guess you autoload the library
Code:
function _userfile(){
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        
        $this->upload->initialize($config);

        if (!$this->upload->do_upload('userfile'))
        {
            $this->validation->set_message('_userfile',$this->upload->display_errors());

            return false;
        } else {
            return true;
        }
    }
It's true validation is only for $_POST values but the upload is based on the $_FILES global so it should work like you say.
#6

[eluser]wiredesignz[/eluser]
This works, but you must fake a $_POST value for 'userfile' to force validation to run
Code:
class Welcome extends Controller
{
    function Welcome()
    {
        parent::Controller();
        
        $this->load->library('validation');
        
        $fields['userfile'] = 'Userfile';
        $this->validation->set_fields($fields);
        
        $rules['userfile'] = 'callback__userfile';
        $this->validation->set_rules($rules);
        
        $this->load->library('upload');
        
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        
        $this->upload->initialize($config);
    }
    
    function index()
    {
        $data['error'] =& $this->validation->error_string;
        $this->load->view('welcome_view', $data);
    }
    
    function do_upload()
    {
        $_POST['userfile'] = 'uploadfile'; // fake the $_POST or use a hidden field in the form
        
        $this->validation->run();
        
        $this->index();
    }

    function _userfile()
    {
         if (!$this->upload->do_upload('userfile'))
        {
            $this->validation->set_message('_userfile', $this->upload->display_errors());

            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}
Use the example View from the User Guide as your welcome_view.php
#7

[eluser]Isos[/eluser]
[quote author="xwero" date="1207244643"]Isos it's a great idea but you have no configuration. I guess you autoload the library
Code:
function _userfile(){
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        
        $this->upload->initialize($config);

        if (!$this->upload->do_upload('userfile'))
        {
            $this->validation->set_message('_userfile',$this->upload->display_errors());

            return false;
        } else {
            return true;
        }
    }
It's true validation is only for $_POST values but the upload is based on the $_FILES global so it should work like you say.[/quote]
Thanks Smile I have already set the configurations inside the constructor function.

I have decided to separate the two forms as my work requires faster solutions for now. But I will try out the fake POST Smile

Thanks for your help guys.
#8

[eluser]Kenzie[/eluser]
I use this to check if there was a file selected, before faking the POST to execute the callback:

Code:
if(isset($_FILES['userfile']) && !empty($_FILES['userfile']['name'])) $_POST['userfile'] = 'uploadfile'; // fake the $_POST or use a hidden field in the form
#9

[eluser]Colin Williams[/eluser]
Let's not forget about the PHP manual:

Quote:$_FILES['userfile']['error']
The error code associated with this file upload. This element was added in PHP 4.2.0

I imagine that would make sense to check! Smile
#10

[eluser]Bramme[/eluser]
I'm with Colin, more specifically: $_FILES['userfile']['error'] == 4 means there was no file selected.




Theme © iAndrew 2016 - Forum software by © MyBB