1

I'm new to Codeigniter's Upload library, but I am familiar with the standard form library. My aim is to instead of use the standard upload library error messages (example: The file you are attempting to upload is larger than the permitted size.) use my own error messages.

In the standard form validation I could use the following code:

$this->form_validation->set_message('required', 'The %s is a required field. Please ensure this field contains the relevant data before continuing.');

However this method for the Upload Library doesn't seem to be the same as i'm attempting to instead of outputting the standard 'max_size' (upload form config rule) error message i want to use my own that actually includes the max size.

I was unable to find anything on this through Codeigniter's documentation, I will show you the code i am using in both my controller and view incase it is of any help:

controller:

$this->form_validation->set_rules('userfile', 'New Image', 'trim|callback_valid_upload'); 

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1';
$config['max_width']  = '1024';
$config['max_height']  = '768';
$config['encrypt_name'] = TRUE;

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

$this->upload->initialize($config);

$data['upload_data'] = '';

if (!$this->upload->do_upload('userfile')) {
    $this->data['error'] = $this->upload->display_errors('<p>', '</p>');
} else { //else, set the success message
    $this->data['success'] = "Upload success!";

    $this->data['upload_data'] = $this->upload->data();

}

view (to output errors):

if (isset($upload_data)) {
  foreach($upload_data as $key => $value) { ?>
    <li><?=$key?>: <?=$value?></li>
  <?php 
  }
}

So in short where i have my config items such as $config['max_size'] = '1'; in my controller i would like to set and error message for it and this doesn't seem to work: $this->form_validation->set_message('max_size', 'The file you have uploaded exceeds the %s MB file size.');

Thanks

3
  • Check out ellislab.com/forums/viewthread/176029 and stackoverflow.com/questions/13172744/… Commented Jun 7, 2014 at 17:17
  • Is there a way without directly changing the $langs ? Commented Jun 7, 2014 at 17:40
  • Define custom error message in the controller or in your own custom error file and append the relevant error as an <li> to an $error array. On the front end do a foreach loop an echo each $error Commented Jun 7, 2014 at 18:45

1 Answer 1

1

You could try and example some thing like this for custom error messages.

if(file_exists(dirname(FCPATH) . '/install/index.php')) {
  $data['error_install'] = $this->lang->line('error_install');
} else {
  $data['error_install'] = '';
}
Sign up to request clarification or add additional context in comments.

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.