Codeigniter文件上传自定义错误消息 [英] Codeigniter File Upload Custom Error Messages

查看:60
本文介绍了Codeigniter文件上传自定义错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.');

但是,用于上载库的此方法似乎与我尝试的方法不同而不是输出标准的 max_size(上传表单配置规则)错误消息,我想使用自己的实际上包含最大大小的消息。

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.

我找不到任何东西通过Codeigniter的文档,我将向您展示我在控制器中使用的代码,并查看它是否有帮助:

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:

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

}

视图(输出错误):

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','您上传的文件超出了%s MB文件的大小。');

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.');

谢谢

推荐答案

您可以尝试举例说明如下内容

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

这篇关于Codeigniter文件上传自定义错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆