3
function submit_article() {
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<p style="color:red">', '<br/></p>');

    $my_rules = array(
        array(
            'field' => 'title',
            'label' => 'Title',
            'rules' => 'required|min_length[5]|max_length[20]|xss_clean'
        ),
        array(
            'field' => 'additionalUpload',
            'label' => 'Additional Upload',
            'rules' => 'callback_is_image'
        )
    );

    $this->form_validation->set_rules($my_rules);

    if ($this->form_validation->run() == FALSE) {
        //ERROR
        $data['title'] = ucfirst('submit Article');
        $this->load->view('templates/header', $data);
        $this->load->view('submit_article', $data);
        $this->load->view('templates/footer', $data);
    } else {
        //SUCCESS
        $data['title'] = ucfirst('article Submitted');
        $this->load->view('templates/header', $data);
        $this->load->view('forms_view/submit_article_success', $data);
        $this->load->view('templates/footer', $data);
    }
}

function is_image($value) {

    $config['upload_path'] = './public/uploads/';
    $config['allowed_types'] = 'gif|jpg|png|pdf|tiff';
    $config['max_size'] = '2048';
    $config['max_width'] = '0';
    $config['max_height'] = '0';
    $config['remove_spaces'] = true;

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

    if (!$this->upload->do_upload()) {
        $this->form_validation->set_message('is_image', $this->upload->display_errors('<p style="color:red">', '<br/></p>'));
        return FALSE;
    } else {
        $this->upload->data();
        return TRUE;
    }
}

Hi every one, this is my controller function code for processing multipart form data in codeigniter, actually the field additionalUpload is not a required feild, but I want it to be validated if the user upload file in additionalUpload field of file type, when I run the above code and click on submit button without selecting any file its shows me error "You did not select a file to upload." which I do not want because this is not a required field, this is my first problem..

and second one is that when I select a file and click on submit button it again show me that "You did not select a file to upload.".

Note: I have just shown two fields of my form here that is title,additionalUpload but I have all total 9 fields.

THANKS IN ADVANCE PLEASE HELP ANYONE.

2
  • $this->upload->do_upload('your_field_name') Commented Nov 24, 2013 at 18:54
  • Thanks its work for me....this solve my second problem..but my first problem is still not solved..please help me @dianuj sir Commented Nov 24, 2013 at 18:57

2 Answers 2

3

The first thing you have to put the name of the file field.

<input type="file" name="image"/>
$this->upload->do_upload('image');

Second, you can not have the max_width and max height 0

$config['max_width']    = '2048';
$config['max_height']   = '2048';

Try that first and then see

For validate field file:

if($_FILES['you_field_name']['tmp_name']){

       //your code  
}

A greeting

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

1 Comment

Thank you for your important time.. but we can have widht and height to 0 for unlimitedness.. now my problem is that this field not required this is an addtional field as name suggest.. so it should not show any error message when users had not selected any file.. but in my case it is showing the error i dont want this..any suggestion from your side?
2

Try this one check if $_FILES is not empty then do the validation else do nothing

 $my_rules = array(
        array(
            'field' => 'title',
            'label' => 'Title',
            'rules' => 'required|min_length[5]|max_length[20]|xss_clean'
        )
    );

if(!empty($_FILES)){

     $my_rules[]= array(
            'field' => 'additionalUpload',
            'label' => 'Additional Upload',
            'rules' => 'callback_is_image'
        )
}

$this->form_validation->set_rules($my_rules);

In your upload function you need to specify the field name

$this->upload->do_upload('your_field_name')

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.