Php 文件未使用codeigniter的form_upload存储
我正试图用CodeIgniter创建一个图库。然而,当我尝试处理表单时,我收到验证错误“需要特征图像字段”,即使我选择了一个文件。请参见下面的查看代码:Php 文件未使用codeigniter的form_upload存储,php,codeigniter,codeigniter-3,Php,Codeigniter,Codeigniter 3,我正试图用CodeIgniter创建一个图库。然而,当我尝试处理表单时,我收到验证错误“需要特征图像字段”,即使我选择了一个文件。请参见下面的查看代码: <section class="content"> <?php $message = $this->session->flashdata('message'); if (isset($message)) { echo '<div class="
<section class="content">
<?php
$message = $this->session->flashdata('message');
if (isset($message)) {
echo '<div class="alert alert-success">' . $message . '</div>';
$this->session->unset_userdata('message');
}
?>
<?php echo validation_errors(); ?>
<?php echo form_open_multipart(); ?>
<div class="row">
<div class="col-md-8 col-sm-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">
<?php echo empty($gallery->id) ? 'Add A New Gallery' : 'Edit Gallery' . $gallery->name; ?>
</h3>
</div>
<div class="box-body">
<div class="box-body">
<div class="form-group">
<label for="name">Name</label>
<?php echo form_input('name', set_value('name', $gallery->name), 'class="form-control" placeholder="Enter Full Name" autocomplete="off"'); ?>
</div>
</div>
<div class="box-body pad">
<div class="form-group">
<label for="description">Description</label>
<?php echo form_textarea('description', set_value('description', $gallery->description), 'class="form-control textarea" placeholder="Enter description"'); ?>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-4 col-sm-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">
Featured Image
</h3>
</div>
<div class="box-body">
<div class="input-group">
<?php echo form_upload('thumbnail', set_value('thumbnail', $gallery->thumbnail), 'id="imgInp"'); ?>
</div>
</div>
<div class="box-footer">
<?php echo form_submit('submit', 'Add Gallery', 'class="btn btn-block btn-primary"'); ?>
</div>
</div>
</div>
</div>
<?php echo form_close(); ?>
</section>
以下是Galler模型,其中包括规则:
class Gallery_M extends MY_Model{
protected $_table_name = 'gallery';
protected $_order_by = 'datesubmitted';
public $rules_admin = array(
'name' => array(
'field' => 'name',
'label' => 'Name',
'rules' => 'trim|required|xss_clean'
),
'description' => array(
'field' => 'description',
'label' => 'Description',
'rules' => 'trim|required|xss_clean'
),
'thumbnail' => array(
'field' => 'thumbnail',
'label' => 'Featured Image',
'rules' => 'trim|required|xss_clean'
)
);
public function get_new(){
$gallery = new stdClass();
$gallery->name ='';
$gallery->description = '';
$gallery->views = '';
$gallery->thumbnail = '';
$gallery->datesubmitted = date('Y-m-d');
return $gallery;
}
public function set_submitted(){
$this->db->where('datesubmitted <=', date('Y-m-d'));
}
}
类库扩展了我的模型{
受保护的$表名称='gallery';
受保护的$_order_by='datesubmitted';
public$rules\u admin=array(
'name'=>数组(
'字段'=>'名称',
“标签”=>“名称”,
“规则”=>“修剪|必需| xss|U清洁”
),
'description'=>数组(
'字段'=>'说明',
“标签”=>“说明”,
“规则”=>“修剪|必需| xss|U清洁”
),
“缩略图”=>数组(
“字段”=>“缩略图”,
“标签”=>“特色图片”,
“规则”=>“修剪|必需| xss|U清洁”
)
);
公共函数get_new(){
$gallery=新的stdClass();
$gallery->name='';
$gallery->description='';
$gallery->views='';
$gallery->缩略图=“”;
$gallery->datesubmitted=日期('Y-m-d');
返回$gallery;
}
公共函数集_已提交(){
$this->db->where('datesubmitted问题是通过多部分表单发送的
文件不能通过$this->input->post()
访问,因此无法使用表单验证进行验证,因为表单验证
没有“看到”它们
如果需要验证文件是否正确上载,可以使用:
if ( ! $this->upload->do_upload('userfile'))
{
// handle the error. You may get CI's error messages with
// $this->upload->display_errors();
// which covers both the existance or lack thereof of an uploaded file, as well as compliance with the upload constraints set by you when initializing the library
}
在上面的示例中,userfile
是在我的表单上输入的文件名。在您的例子中,它应该是thumbnail
常规的表单\u验证仍然适用于表单的其他字段。您可以在单个数组中对这两个字段进行分组,如下所示:
$data = array(
'upload_data' => $this->upload->data(),
'form_data' => $this->input->post(),
);
试一试,让我知道它是否工作我在视图中没有看到任何错误。将控制器的代码添加到您的问题中。在表单的呈现HTML中,
标记是否设置了适当的enctype以提供多部分文件上载?
@Ivan呈现如下:@DFriend我添加了控制器功能nSimilar here:[link]通过删除缩略图规则,它处理了表单。感谢您的帮助
$data = array(
'upload_data' => $this->upload->data(),
'form_data' => $this->input->post(),
);