1

I've created validation rules for image uploading as follows.

$this->form_validation->set_rules('display_photo', 'Display Photo', 'callback_file_required|callback_file_size_max[1000]');

In controller I've included callback function as follows.

     public function file_required($display_photo) {

        if($_FILES[$display_photo]['size'] == 0) {
            $this->form_validation->set_message('file_required', 'Upload a file.');
            return false;
        }
        return true;
     }

      public function file_size_max($display_photo, $max_size) {

        if($_FILES[$display_photo]['size'] > $max_size) {
            $this->form_validation->set_message('file_size_max', 'This file exceeds max size.');
            return false;
        }
        return true;
      }

Only first rule is executing but not second rule. Please help me to find out the solution.

Sanganabasu
  • 943
  • 7
  • 21
  • 39

5 Answers5

0

You should pass the file size limit in the config not in the validation rule. Can u please paste the function or method that performs the upload ?

Chiribuc
  • 88
  • 1
  • 1
  • 8
  • Just validating the image not written code for uploading yet. Anyway If I'm passing argument that function should work right. – Sanganabasu Nov 26 '13 at 09:22
0
$this->form_validation->set_rules('display_photo', 'Display Photo', 'callback_file_required_with_validation');

 public function file_required_with_validation($display_photo) {

    $max_size=1000;

    if($_FILES[$display_photo]['size'] == 0) {
        $this->form_validation->set_message('file_required', 'Upload a file.');
        return false;
    }

    if($_FILES[$display_photo]['size'] > $max_size) {
        $this->form_validation->set_message('file_size_max', 'This file exceeds max size.');
        return false;
    }     
    return true;
 }
ashatte
  • 5,442
  • 8
  • 39
  • 50
Dilip kumar
  • 176
  • 6
0

Not sure if you can use two callbacks on a single field when validation forms in CI. In any case you don't need to check the size to see if a file was uploaded. Use error code '4'

Solutions:

  • Check if the file was uploaded with if($_FILES[$display_photo]['error'] !== 4) // Error code 4 means no file was uploaded. So if it's not 4 then you have a file.
  • If you want to do multiple checks you can put them both in one single callback
stef
  • 26,771
  • 31
  • 105
  • 143
0

Pretty sure you need double underscore after 'callback' in your validation definition...

$this->form_validation->set_rules('display_photo', 'Display Photo', 'callback__file_required|callback__file_size_max[1000]');
RaggaMuffin-420
  • 1,762
  • 1
  • 10
  • 14
-1

One way - You can validate with JS before uploading the file:

<form method="post" enctype="multipart/form-data" action="upload.php">
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>

<script>
document.forms[0].addEventListener('submit', function( evt ) {
    var file = document.getElementById('file').files[0];

    if(file && file.size < 10485760) { // 10 MB (this size is in bytes)
        //Submit form        
    } else {
        //Prevent default and display error
        evt.preventDefault();
    }
}, false);
</script>

Original Question is here

But if you want validate at server side, then server until does't know whenever we upload the file.

Community
  • 1
  • 1
Suleman Ahmad
  • 2,025
  • 4
  • 28
  • 43