Welcome Guest, Not a member yet? Register   Sign In
[ SOLVED ] File upload input doesn't validate
#1

[eluser]GreenDude[/eluser]
Hi there.

Just finished reading most of the the CI docs today (started this morning) and I wanted to build a site which features file uploads among other things. so I rounded this up:

Code:
class Face extends Controller {

    function __construct()
    {
        parent::Controller();
        
        $db_users = $this->load->database('users', TRUE);
        $db_data = $this->load->database('data', TRUE);
        
        $this->load->helper(array('form', 'url'));
    }

    function index()
    {
        $headdata['title'] = "MedieMareLaInfo";
        $headdata['heading'] = "MedieMareLaInfo";
        $headdata['base_url'] = $this->config->item('base_url');
        
        $this->load->view('header', $headdata); echo $this->output->get_output();
        $this->load->view('faceview'); echo $this->output->get_output();
        $this->load->view('footer'); echo $this->output->get_output();
        
    }
    
    function dashboard()
    {
        $headdata['title'] = "MedieMareLaInfo Admin";
        $headdata['heading'] = "MedieMareLaInfo Admin";
        $headdata['base_url'] = $this->config->item('base_url');
        $data['error'] = '';
        
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        
        $this->load->library('validation');
        $this->validation->set_error_delimiters('<div class="error">', '</div>');
        
        $rules['username']    = "required";
        $rules['password']    = "required";
        $rules['passconf']    = "required";
        $rules['email']        = "required";
        $rules['userfile']    = "required";
        
        $this->validation->set_rules($rules);
        
        $fields['username']    = 'Username';
        $fields['password']    = 'Password';
        $fields['passconf']    = 'Password Confirmation';
        $fields['email']    = 'Email Address';
    
        $this->validation->set_fields($fields);
        
        if ($this->validation->run() == FALSE)
        {
            $this->load->view('header', $headdata); echo $this->output->get_output();
            $this->load->view('dashboard'); echo $this->output->get_output();
            $this->load->view('upload_form', $data); echo $this->output->get_output();
            $this->load->view('footer'); echo $this->output->get_output();
        }
        
        else
        {
            /*
            $this->load->library('upload', $config);
        
            if ( ! $this->upload->do_upload())
            {
                $data['error'] = $this->upload->display_errors();
            }*/
            
            $this->load->view('header', $headdata); echo $this->output->get_output();
            $this->load->view('dashboard'); echo $this->output->get_output();
            $this->load->view('upload_success'); echo $this->output->get_output();
            $this->load->view('footer'); echo $this->output->get_output();
        }
    }

The problem is the file won't validate.. it keeps saying the field is empty. I've searched for an hour and still haven't found the reason. Smile

Can you help me a bit please ?

CI is wonderful so far, by the way. Smile


GreenDude
#2

[eluser]xwero[/eluser]
userfile is not in the $_POST array but in the $_FILES array. The validation of the upload happens during the do_upload method. You have to do something like
Code:
$errors = '';
if ($this->validation->run() == FALSE)
{
    $errors = $this->validation->error_string;
}
if(! $this->upload->do_upload())
{
    $errors = implode('<br>',$this->upload->display_errors());
}
if($errors != '')
{
    $this->load->view('header', $headdata); echo $this->output->get_output();
            $this->load->view('dashboard'); echo $this->output->get_output();
            $this->load->view('upload_form', $data); echo $this->output->get_output();
            $this->load->view('footer'); echo $this->output->get_output();
}
else
{
     $this->load->view('header', $headdata); echo $this->output->get_output();
            $this->load->view('dashboard'); echo $this->output->get_output();
            $this->load->view('upload_success'); echo $this->output->get_output();
            $this->load->view('footer'); echo $this->output->get_output();
}
But why are you doing echo $this->output->get_output(); ?
#3

[eluser]GreenDude[/eluser]
Thanks for the reply. Forgot about that array Smile

I changed it to this and now it's working pretty fine:

Code:
$this->validation->set_fields($fields);
        
        if ($this->validation->run() == FALSE || !$this->upload->do_upload())
        {
            $data['error'] = $this->upload->display_errors();
                
            $this->load->view('header', $headdata); echo $this->output->get_output();
            $this->load->view('dashboard'); echo $this->output->get_output();
            $this->load->view('upload_form', $data); echo $this->output->get_output();
            $this->load->view('footer'); echo $this->output->get_output();
        }
        else
        {
            $this->load->view('header', $headdata); echo $this->output->get_output();
            $this->load->view('dashboard'); echo $this->output->get_output();
            $this->load->view('upload_success'); echo $this->output->get_output();
            $this->load->view('footer'); echo $this->output->get_output();    
        }




Theme © iAndrew 2016 - Forum software by © MyBB