php - 辅助函数在 CodeIgniter 中无法正常工作
问题描述
我创建了一个帮助函数调用每个文件的函数。基本上,该函数正在检查上传的文件是否为 pdf/xlsx,但该函数没有返回任何结果。我无法弄清楚错误在哪里。
我的控制器
class Bike_insurance_brochure extends CI_Controller {
function __construct(){
parent::__construct();
if(!is_admin_logged_in())
redirect("/admin","refresh",301);
$this->load->model('admin/bike_insurance_brochure_m');
}
function index(){
$this->load->admin_template('bike_insurance_brochure_view');
}
function save(){
$config=array(
array(
'field'=>'product_b',
'label'=>'Product Brochure',
'rules'=>'trim|callback_check_brochure'
),
array(
'field'=>'product_w',
'label'=>'Policy Wordings',
'rules'=>'trim|callback_check_word'
),
array(
'field'=>'product_c',
'label'=>'Policy Claim',
'rules'=>'trim|callback_check_claim'
),
array(
'field'=>'company',
'label'=>'company',
'rules'=>'trim|max_length[255]'
),
array(
'field'=>'product',
'label'=>'product',
'rules'=>'trim|required'
)
);
$this->form_validation->set_rules($config);
if($this->form_validation->run()!==FALSE){
print_r(json_encode($this->bike_insurance_brochure_m->save()));
}
else{
$error= "<ul>";
$error.= validation_errors("<li>", "</li>");
$error.= "</ul>";
print_r(json_encode(['status'=>'false','message'=>$error]));
}
}
function get_product_details($bike_insu_bro_id=NULL){
if($bike_insu_bro_id==NULL || trim($bike_insu_bro_id)==""){
print_r(json_encode(['status'=>'false','message'=>'Invalid record selected']));
}
print_r(json_encode($this->bike_insurance_brochure_m->get_product_details($bike_insu_bro_id)));
}
function update_details($bike_insu_bro_id){
$config=array(
array(
'field'=>'product_b',
'label'=>'Product Brochure',
'rules'=>'trim|callback_check_brochure'
),
array(
'field'=>'product_w',
'label'=>'Policy Wordings',
'rules'=>'trim|callback_check_word'
),
array(
'field'=>'product_c',
'label'=>'Policy Claim',
'rules'=>'trim|callback_check_claim'
),
array(
'field'=>'company',
'label'=>'company',
'rules'=>'trim|max_length[255]'
),
array(
'field'=>'product',
'label'=>'product',
'rules'=>'trim|required'
)
);
$this->form_validation->set_rules($config);
if($this->form_validation->run()!==FALSE){
print_r(json_encode($this->bike_insurance_brochure_m->update_details($bike_insu_bro_id)));
}
else{
$error= "<ul>";
$error.= validation_errors("<li>", "</li>");
$error.= "</ul>";
print_r(json_encode(['status'=>'false','message'=>$error]));
}
}
function delete($bike_insu_bro_id=NULL){
if($bike_insu_bro_id==NULL || trim($bike_insu_bro_id)==""){
print_r(json_encode(['status'=>'false','message'=>'Invalid record selected']));
}
print_r(json_encode($this->bike_insurance_brochure_m->delete($bike_insu_bro_id)));
}
function get_bike_insurance_brochure_list($page=1){
$this->load->library('pagination');
$this->load->library('paginationlib');
try
{
$pagingConfig = $this->paginationlib->initPagination($this->bike_insurance_brochure_m->record_count());
$list = $this->bike_insurance_brochure_m->get_bike_insurance_brochure_list((($page-1) * $pagingConfig['per_page']),$pagingConfig['per_page']);
$list["pagination"] = $this->pagination->create_links();
$list['index_start']=(50*($page-1))+1;
print_r($this->load->view('admin/ajax/bike_insurance_brochure_list',$list,TRUE));
}
catch (Exception $err){}
}
function get_bike_product_list(){
print_r(json_encode($this->bike_insurance_brochure_m->get_product_list($this->input->post('company'))));
}
function check_brochure(){
check_brochure($this->input->post('product_b'));
}
function check_word(){
check_word($this->input->post('product_w'));
}
function check_claim(){
check_claim($this->input->post('product_c'));
}
}
自定义辅助函数
if (!function_exists('check_brochure'))
{
function check_brochure($product_brochure){
$CI =& get_instance();
$allowed_mime_type_arr = array("application/vnd.openxmlformats-officedocument.wordprocessingml.document","application/pdf","application/msword");
$mime_brochure = get_mime_by_extension($product_brochure);
if(isset($product_brochure) && $product_brochure !=""){
if(in_array($mime_brochure, $allowed_mime_type_arr)){
echo 'hi';
return true;
}else{
$CI->form_validation->set_message('check_brochure', 'Please select only .pdf/.docx/.doc Product Brochure.');
echo 'ye';
return false;
}
}else{
$CI->form_validation->set_message('check_brochure', 'Please choose a file to upload Product Brochure.');
echo 'bye';
return false;
}
}
}
它在检查时返回再见,但功能未返回 false,我的意思是错误消息未显示。我的自动加载.php:
$autoload['helper'] = array('url', 'file','form','email_settings','comm_func','html','string','globals');
解决方案
对于上传,您将无法使用form_validation
它,因为它仅适用于常规表单字段(通过 可访问的任何内容$this->input->post("field_name")
)。对于通过表单上传的文件(您可以通过 访问)的任何内容$this->upload->data()
,您需要使用上传库的配置。
在 Codeigniter 上,所有多部分表单都以这种方式工作。我通常通过以下方式简化我的工作流程:
$data = array(
'upload_data' => $this->upload->data(),
'form_data' => $this->input->post(),
);
所以我访问所有内容$data['form_data']['field_name']
和$data['upload_data']['upload_parameter']
(upload_parameter
作为 CI 自动为你做的许多事情之一)
试试这个例子......它只是一个快速类型,你可以改进它很多:(在我的例子中,文件字段被称为'userfile')
// define your settings
$config['upload_path'] = wherever/you/need/the/file/to/end/up;
$config['allowed_types'] = 'pdf|xls|xlsx';
// initialize the upload library
$this->load->library('upload', $config);
// do_upload actually uploads the file and checks that the configuration settings you defined are met
if (!$this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
print "<pre>".print_r($error,true)."</pre>";
尝试这样做,你就会让它工作(并且不需要使用辅助函数,因为内置的 CI 功能已经为你处理了几乎所有事情)
推荐阅读
- python - 如何在熊猫中将对象转换为int或float
- android - 带感叹号的 Kotlin 数据类型
- javascript - 可调用的 Firebase 函数在单击按钮时没有任何反应
- python - 试图从另一个文件导入一个类,但我的测试中断
- reactjs - 没有未使用的表达式 Reactjs 请帮助我
- c++ - 多线程 C++ winsock 套接字中重复和丢失消息的原因?
- reactjs - Prettier 在 setState 中移动 paren,触发无序列。如何改进 setState?
- javascript - 如何调用组件的 onClick 属性进行测试?
- regex - 使用 grep -P 和lookahead/lookbehind 获取模式之间的文本
- javascript - 在 JavaScript 中,我不能为字符串数组创建唯一值,也不能从另一个数组中排除确定的值