mdixon18
mdixon18

Reputation: 1219

Codeigniter File Upload Custom Error Messages

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

Upvotes: 1

Views: 5310

Answers (1)

user3300065
user3300065

Reputation:

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'] = '';
}

Upvotes: 1

Related Questions