1

I have a form with text inputs and a file input. What is a proper way validate both input types using Codeigniter's validation library? I found some solutions but they don't work properly or seem like an overkill (creating new libraries or modifying CI system files).

In my view I'm using 1 multipart form and displaying both text validation error and upload errors.

Here is what I have so far in my Controller...

function create() //create new post
{           
    $this->form_validation->set_rules('content', 'Entry', 'trim|required|xss_clean');
    $this->form_validation->set_rules('category_id', 'Category', 'trim|required|xss_clean|integer'); 

    //Text input fields
    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('new_post');
    }       
    else
    {
            $config['upload_path'] = './uploads/posts/';
            $config['allowed_types'] = 'jpg|png';               
            $config['max_size'] = '800'; //in KB

            $this->load->library('upload', $config);

            //File Upload
            if (! $this->upload->do_upload())
            {
                $upload_error['upload_error'] = array('error' => $this->upload->display_errors()); 

                $this->load->view('my_view', $upload_error);

                return FALSE;
            }

             //Add to database 
             $data = array (
               'user_id' => $this->tank_auth->get_user_id(),
               'category_id' => $this->input->post('category_id'),
               'content' => $this->input->post('content')
             );

             $this->Posts_model->create_post($data);

             $this->session->set_flashdata('success', 'Post_added!');
             redirect('posts');
    }       

}

I keep getting You did not select a file to upload. in my view.

4 Answers 4

4

What is the name of your file input? do_upload() is expecting it by default to be 'userfile', however if you have something like <input type="file" name="image"... you will need to call $this->upload->do_upload('image')

You also need to make sure the form is set to multipart - http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!! I did not set the name of my file input.
2

You're using CodeIgniter's helpers correctly, this could be running more into a PHP problem then a CodeIgniter problem.

It looks like your file may be too big for your PHP configuration? PHP doesn't seem to be passing the file along to you. Create a php_info() file and see what UPLOAD_MAX_FILESIZE is set to?

Also, make sure you have the extension and mime type pair set in application/config/mimes.php.

2 Comments

Thanks, I'm trying to upload allowed file size and type and still get the message. When I try to upload a file exceeding the max filesize I should get that error. Uploading works in my application when just processing type file.
Setting the extension/mime type pair fixed the problem for me.
0

default codeigneter upload class is $this->upload->do_upload('field_name = userfile') , so you can set the name of field file you make or you use default codeigneter name field of file , if you want to change the name field for file type in your form , please check how codeigneter made setting for that class, actually is simple class and easy to use.., because that class need parameter name of file

Comments

0

Form field validation with file upload using codeigniter

Controller: Upload.php

Note: create folder uploads at base level Upload folder store file..

<?php

class Upload extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    function index()
    {
        //$this->load->view('upload_form', array('error' => ' ' ));
        $this->load->library('form_validation');
        $this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');

        //Text input fields
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('upload_form');
        }
        else
        {
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1024';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';

            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors());

                $this->load->view('upload_form', $error);
            }
            else
            {
                $data = array('upload_data' => $this->upload->data());

                $this->load->view('upload_success', $data);
            }
        }

    }
}
?>

Form:(upload.form.php)

<html>
<head>
<title>Upload Form</title>
</head>
<body>

<?php echo validation_errors(); ?>

<?php echo form_open_multipart();?>
Title: <input type="text" name="title" size="30" /><br/>
<input type="file" name="userfile" size="20" />

<br /><br />

<input type="submit" value="upload" />

</form>

</body>
</html>

**upload_success.php**


<html>
<head>
<title>Upload Form</title>
</head>
<body>

<?php echo validation_errors(); ?>

<?php echo form_open_multipart();?>
Title: <input type="text" name="title" size="30" /><br/>
<input type="file" name="userfile" size="20" />

<br /><br />

<input type="submit" value="upload" />

</form>

</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.