文件未与form_upload代码点火器一起存储



我正在尝试使用CodeIgniter创建一个图库。但是,当我尝试处理表单时,即使我选择了文件,我也会收到验证错误"特色图像字段是必需的"。请参阅下面的查看代码:

<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>

我尝试多次重新创建表单,但不断收到相同的错误。我尝试了form_open并得到了相同的结果。有什么建议吗? 控制器如下:

public function edit($id = null){
// Fetch a user or set a new one
if ($id) {
$this->data['page_title'] = 'Edit Gallery';
$this->data['user'] = $this->gallery_m->get($id);
count($this->data['gallery']) || $this->data['errors'][] = 'Gallery could not be found';
}else{
$this->data['page_title'] = 'Add New Gallery';
$this->data['gallery'] = $this->gallery_m->get_new();
}
// Set up the form
$rules = $this->gallery_m->rules_admin;
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == TRUE) {
if ( !$this->upload->do_upload('thumbnail')){
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}else{
//file is uploaded successfully
//now get the file uploaded data 
$imagedata = $this->upload->data();
//get the uploaded file name
$data['pic_file'] = $imagedata['file_name'];
//store pic data to the db
$this->pic_model->store_pic_data($data);
$this->session->set_flashdata( 'message', 'Gallery Added Successfully' );
}
}
// Load the view
$this->data['subview'] = 'admin/gallery/edit';
$this->load->view( 'admin/body', $this->data );
}

以下是包括规则的画廊模型:

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

问题是通过多部分表单发送的files无法通过$this->input->post()访问,因此无法使用form_validation进行验证,因为form_validation不会"看到"它们。

如果您需要验证文件是否已正确上传,您可以使用:

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.

常规form_validation仍适用于表单的其他字段。您可以将两者分组到单个数组中,如下所示:

$data = array(
'upload_data'   => $this->upload->data(),
'form_data'     => $this->input->post(),
);

试一试,让我知道它是否有效

相关内容

  • 没有找到相关文章

最新更新