【发布时间】: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->form_validation->set_message('max_size', 'The file you have uploaded exceeds the %s MB file size.');
谢谢
【问题讨论】:
-
有没有不直接改变 $langs 的方法?
-
在控制器或您自己的自定义错误文件中定义自定义错误消息,并将相关错误作为
- 附加到
$error数组。在前端做一个foreach循环回显每个$error - 附加到
标签: php codeigniter