首页 > 解决方案 > 文件未与带有 codeigniter 的 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 );
    }

以下是包含规则的 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'));
    }
}

标签: phpcodeignitercodeigniter-3

解决方案


问题是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(),
);

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


推荐阅读