我正在使用 CodeIgniter 通过表单上传一些文件。我知道如何翻译“常规”表单错误(required、valid_email 等),但我不知道如何处理文件错误(文件不允许、文件太大等)。
我应该使用哪些键
$this->form_validation->set_message('KEY', 'TRANSLATION');
?
谢谢
我正在使用 CodeIgniter 通过表单上传一些文件。我知道如何翻译“常规”表单错误(required、valid_email 等),但我不知道如何处理文件错误(文件不允许、文件太大等)。
我应该使用哪些键
$this->form_validation->set_message('KEY', 'TRANSLATION');
?
谢谢
方法一
默认情况下,语言文件通常存储在system/language
目录中。或者,您可以在文件夹中创建一个名为upload_lang.php
的application/language
文件并将它们存储在那里。
$lang['upload_userfile_not_set'] = "Unable to find a post variable called userfile.";
$lang['upload_file_exceeds_limit'] = "The uploaded file exceeds the maximum allowed size in your PHP configuration file.";
$lang['upload_file_exceeds_form_limit'] = "The uploaded file exceeds the maximum size allowed by the submission form.";
$lang['upload_file_partial'] = "The file was only partially uploaded.";
$lang['upload_no_temp_directory'] = "The temporary folder is missing.";
$lang['upload_unable_to_write_file'] = "The file could not be written to disk.";
$lang['upload_stopped_by_extension'] = "The file upload was stopped by extension.";
$lang['upload_no_file_selected'] = "You did not select a file to upload.";
$lang['upload_invalid_filetype'] = "The filetype you are attempting to upload is not allowed.";
$lang['upload_invalid_filesize'] = "The file you are attempting to upload is larger than the permitted size.";
$lang['upload_invalid_dimensions'] = "The image you are attempting to upload exceedes the maximum height or width.";
$lang['upload_destination_error'] = "A problem was encountered while attempting to move the uploaded file to the final destination.";
$lang['upload_no_filepath'] = "The upload path does not appear to be valid.";
$lang['upload_no_file_types'] = "You have not specified any allowed file types.";
$lang['upload_bad_filename'] = "The file name you submitted already exists on the server.";
$lang['upload_not_writable'] = "The upload destination folder does not appear to be writable.";
方法二
您可以在上传过程后使用Flashdata 。
if ($this->upload->do_upload())
{
$this->session->set_flashdata('success', 'Yep! Upload complete');
redirect('go_back_home');
}
else
{
$this->session->set_flashdata('error', 'Uh, upload not complete! The problem is..'); // Manually check
redirect('go_back_form');
}
您也可以使用$this->upload->data()
. 这是一个帮助函数,它返回一个数组,其中包含与您上传的文件相关的所有数据。这是数组原型:
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
尝试这个:
lang:your_translation_key
一个真实的例子:
$this->form_validation->set_rules('name', 'lang:web_name', 'required|trim|xss_clean|min_length[2]|max_length[100]');