Codeigniter文件上载自定义错误消息

时间:2014-06-07 16:52:11

标签: php codeigniter

我是Codeigniter上传库的新手,但我熟悉标准表单库。我的目标是不使用标准上传库错误消息(例如:您尝试上传的文件大于允许的大小。)使用我自己的错误消息。

在标准表单验证中,我可以使用以下代码:

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

然而,上传库的这种方法似乎与我试图不输出标准' max_size' (上传表单配置规则)错误消息我想使用我自己的实际包含最大大小。

我无法通过Codeigniter的文档找到任何相关内容,我将向您展示我在我的控制器和视图中使用的代码,如果它有任何帮助:

控制器:

$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();

}

查看(输出错误):

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

所以,简而言之,我在我的控制器中有$config['max_size'] = '1';的配置项,我想为它设置并显示错误信息,这似乎不起作用:$this->form_validation->set_message('max_size', 'The file you have uploaded exceeds the %s MB file size.');

由于

1 个答案:

答案 0 :(得分:1)

您可以尝试为自定义错误消息示例这样的事情。

if(file_exists(dirname(FCPATH) . '/install/index.php')) {
  $data['error_install'] = $this->lang->line('error_install');
} else {
  $data['error_install'] = '';
}