【问题标题】:Codeigniter File Upload Custom Error MessagesCodeigniter 文件上传自定义错误消息
【发布时间】:2014-06-07 16:52:11
【问题描述】:

我是 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-&gt;form_validation-&gt;set_message('max_size', 'The file you have uploaded exceeds the %s MB file size.');

谢谢

【问题讨论】:

标签: php codeigniter


【解决方案1】:

您可以尝试为自定义错误消息提供类似的示例。

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

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签