即使选择了文件,也会显示CodeIgniter文件验证
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了即使选择了文件,也会显示CodeIgniter文件验证相关的知识,希望对你有一定的参考价值。
我遇到了大麻烦,是CI的新手。我没有发现我的错误。我只是尝试上传需要验证的文件,但如果我选择该文件,它仍会显示所需的验证错误消息。休息验证对我来说很好。当我没有选择文件时,它显示错误'请选择文件'。这是正确的,但即使在我选择文件后也会显示相同的错误。我不明白,我完全错过了什么。这是我的代码。
$this->form_validation->set_rules('list_name', 'List Name', 'required');
$this->form_validation->set_rules('name_product', 'Product Name', 'required');
$this->form_validation->set_rules('description', 'Description','required');
$this->form_validation->set_rules('cost_of_list', 'Cost of list','numeric|required');
$this->form_validation->set_rules('user_file', 'Document', 'callback_file_selected_test');
// Validation for Excel file upload
/* if (empty($_FILES['file']['name'])) {
$this->form_validation->set_rules('file', 'Document', 'required');
} */
if ($this->form_validation->run() == FALSE) {
echo json_encode(array( 'status' => false, 'msg' => validation_errors()));
}
file_selected_test函数
function file_selected_test(){
$this->form_validation->set_message('file_selected_test', 'Please select file.');
if (empty($_FILES['user_file']['tmp_name'])) {
return false;
}else{
return true;
}
}
以下代码是View
<div class="modal fade" id="modal_form" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h3 class="modal-title">List Information</h3>
</div>
<div class="modal-body form">
<div id="validation-error" style="color:red;"></div>
<form action="#" id="form" class="form-horizontal" enctype="multipart/form-data">
<input type="hidden" name="product_id"/>
<div class="form-body">
<div class="form-group">
<div class="col-md-3"> <label class="control-label">List Name</label></div>
<div class="col-md-9">
<input name="list_name" placeholder="List Name" class="form-control" type="text" required="required">
</div>
</div>
<div class="form-group">
<div class="col-md-3"><label class="control-label ">Product Name</label></div>
<div class="col-md-9">
<select class="form-control" name="name_product" placeholder="Product Name" class="form-control" id="name_product" >
<option disabled selected value> -- Select An Option -- </option>
<?php
foreach($products as $product)
{
echo '<option value="'.$product->product_id.'">'.$product->name_product.'</option>';
}
?>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-3"><label class="control-label">Description</label></div>
<div class="col-md-9">
<textarea class="form-control" rows="3" id="comment" name="description" ></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-3"><label class="control-label">Cost of List</label></div>
<div class="col-md-9">
<input type="text" class="form-control" id="" name="cost_of_list" ></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-3"><label class="control-label">Select File</label></div>
<div class="col-md-9">
<input id="input-2" name="user_file" type="file" class="file" >
</div>
</div>
<div class="form-group" id="companyCount" style="display:none;">
<div class="col-md-3"><label class="control-label">No of companies</label></div>
<div class="col-md-9">
<input name="no_of_companies" placeholder="No of companies" class="form-control" type="text" required="required">
</div>
</div>
<div class="form-group" id="contactsCount" style="display:none;">
<div class="col-md-3"><label class="control-label">No of Contacts</label></div>
<div class="col-md-9">
<input name="no_of_contacts" placeholder="No of Contacts" class="form-control" type="text" >
</div>
</div>
<div id="search_rolebase" style="display:none;">
<div class="form-group" >
<div class="col-md-12" style="text-align:center"><label class="control-label">Search criteria for Rolebase</label></div>
</div>
<div class="form-group" id="">
<div class="col-md-3"><label class="control-label">Titles</label></div>
<div class="col-md-9">
<input name="titles" placeholder="Titles" class="form-control" type="text" >
</div>
</div>
<div class="form-group" id="">
<div class="col-md-3"><label class="control-label">Country</label></div>
<div class="col-md-9">
<input name="country" placeholder="Country" class="form-control" type="text" >
</div>
</div>
</div>
<div id="search_accountbase" style="display:none;">
<div class="form-group" >
<div class="col-md-12" style="text-align:center">
<label class="control-label">Search criteria for Account Base</label></div></div>
<div class="form-group" id="">
<div class="col-md-3"><label class="control-label">Region</label></div>
<div class="col-md-9">
<input name="region" placeholder="Region" class="form-control" type="text" >
</div>
</div>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" id="btnSave" onclick="save()" class="btn btn-primary">Save</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
不工作甚至更新到codeigniter功能上传功能显示相同的结果,这是代码
$file_path = realpath(APPPATH . '../uploads/files/');
$config['upload_path'] = $file_path;
$config['allowed_types'] = '*';
// $config['max_size'] = 100;
// $config['max_width'] = 1024;
// $config['max_height'] = 768;
$this->upload->initialize($config);
if(!$this->upload->do_upload('userfile'))
{
echo json_encode(array( 'status' => false, 'msg' => $this->upload->display_errors()));
}
它显示错误“您没有选择要上传的文件”。但是当我单独为上传进行编码时,它工作正常。
答案
Your validation Code:
$this->form_validation->set_rules('list_name', 'List Name', 'required');
$this->form_validation->set_rules('name_product', 'Product Name', 'required');
$this->form_validation->set_rules('description', 'Description','required');
$this->form_validation->set_rules('cost_of_list', 'Cost of list','numeric|required');
$this->form_validation->set_rules('user_file', 'Document', 'callback_file_selected_test');
Try This Way:
$this->form_validation->set_rules('list_name', 'List Name', 'required');
$this->form_validation->set_rules('name_product', 'Product Name', 'required');
$this->form_validation->set_rules('description', 'Description','required');
$this->form_validation->set_rules('cost_of_list', 'Cost of list','numeric|required');
if (empty($_FILES['user_file']['name']))
{
$this->form_validation->set_rules('user_file', 'Document', 'required');
}
试试这个条件:
if($_FILES['userfile']['error'] != 4)
{
return false;
}
$_FILES['userfile']['error'] != 4
用于上传所需的文件。
另一答案
尝试将tmp_name更改为name,因为它在传递值as given in this link时可以为NULL
将代码更改为
function file_selected_test(){
$this->form_validation->set_message('file_selected_test', 'Please select file.');
if (empty($_FILES['user_file']['name'])) {
return false;
}else{
return true;
}
}
也试试这种方式
以上是关于即使选择了文件,也会显示CodeIgniter文件验证的主要内容,如果未能解决你的问题,请参考以下文章
即使未加载 codeigniter 会话库,也会在数据库中创建新的会话条目。为啥?
即使禁用了文本选择,IOs Cordova 长按也会显示文本选择放大镜,如何删除?
即使文件模式显式为 DOS,gVim 也会显示回车符 (^M)
Codeigniter“未指定输入文件”即使粘贴新的.htaccess