File Upload Validation In Codeigniter

Viewed 16092

I am trying to validate file upload for image uploading, but it is not getting the validation like other fields. I am using Form_Validation.php process for validation.

Image uploading array:

array(
            'field'=>'image',
            'label' => 'Image',
            'rules' => 'required'
        )

when i try to upload the image it did not response like it is required etc. I also want to validate it for .jpg etc and "how to set the file value on incorrect file like instead of .jpg we try to upload the .pdf" like we set the value of input field set_value('field name') etc.

I checked a lot of questions and also try to use call method, but did not able to fix it.

UPDATE:

Please provide detail answer with code example. Please use the form_validation.php way in example and also provide the callback example code, so i can read / learn and modify it accordingly.

UPDATE 2:

 public function Task()
    {
        if ($this->form_validation->run('Sub_Admin/task') == FALSE) {
            $this->data['Task'] = $this->bm->get_usr();
            $data['title'] = "Add New Task";
            $this->load->view('Subadmin/header',$data);
            $this->load->view('Subadmin/nav');
            $this->load->view('Subadmin/sidebar');
            $this->load->view('Subadmin/task', $this->data);
            $this->load->view('Subadmin/footer');
        }
        else
        {

            $config['upload_path'] = './taskimages/'; //The path where the image will be save
            $config['allowed_types'] = 'gif|jpg|png'; //Images extensions accepted
            $config['max_size'] ='10048'; //The max size of the image in kb's
            //$config['max_width']  = '1024'; //The max of the images width in px
            //$config['max_height']  = '768'; //The max of the images height in px
            $config['overwrite'] = FALSE; //If exists an image with the same name it will overwrite. Set to false if don't want to overwrite
            $this->load->library('upload', $config); //Load the upload CI library
            $this->load->initialize($config);
            $this->upload->do_upload('task');
            $file_info = $this->upload->data();
            $file_name = $file_info['file_name'];
            $data = array(
                'Job_Title' => $this->input->post('jtitle'),
                'Priority' => $this->input->post('jnature'),
                'Assignee' => $this->input->post('assigne'),
                'Employee_Name' => $this->input->post('assignto'),
                'Due_Date' => $this->input->post('ddate'),
                'Reminder' => $this->input->post('reminder'),
                'Task_Image' => $file_name,
            );

            $this->bm->add_task($data);

        }
    }

I am already using CI uploading class but it is not working, and now i want to validate the image/ file from form_validation side.

8 Answers

In CODEIGNITER 4 the Validation class includes features of file validation. you can achieve it by the following block of code.

in Config/Validation.php add

public $signup = [
        'username' => [
            'rules'  => 'required|alpha',
            'errors' => [
                'required' => 'You must choose a Username.'
            ]
        ],
        'profile_image' => [
            'rules' => 'uploaded[profile_image]|max_size[profile_image,1024]|ext_in[profile_image,png,jpg]|max_dims[profile_image,1000,1000]',
            'label' => 'profile image'
        ]
    ];

in your controller add following code

public function register()
{
    $validation =  \Config\Services::validation();
    if($this->request->getMethod() == 'post') {
        //$validation->setRuleGroup('signup');
        if (!$this->validate('signup')) {
            //$validationErrors = $validation->getErrors();
            echo view('register', [
                'validation' => $validation
            ]);
        }
        else
        {
            // your logic goes here....
            echo view('Success');
        }
    } else {
        echo view('register', [
            'validation' => $validation
        ]);
    }
}

your view will be like

    <?= $validation->listErrors() ?>
<?php echo form_open_multipart(); ?>
<div class="row">
    <div class="col-md-4 offset-md-4">
        <label>Full Name</label><br/>
        <input type="text" name="username" class="form-control" size="50" value="<?php echo set_value('username'); ?>"/>
    </div>
</div>
<div class="row">
    <div class="col-md-4 offset-md-4">
        <label>Profile Picture</label><br/>
        <input type="file" name="profile_image" class="form-control" value="<?= set_value('profile_image') ?>"/>
    </div>
</div>
<div class="row">
    <div class="col-md-4 offset-md-4"><br/>
        <button class="btn btn-primary btn-block">Login</button>
    </div>
</div>
<?php echo form_close(); ?>
Related