在Codeigniter中使用表单数据上传单个文件

时间:2019-02-25 17:19:39

标签: php html css codeigniter file-upload

嗨,我正尝试使用codeigniter上传带有表单数据的图像。显然,单击“提交”按钮后,它会一直提示我未选择任何文件的错误消息。我一直在尝试调试我的代码,但是请您帮我弄清楚问题所在。谢谢!

查看

<div class="right_col" role="main">
  <div class="">
    <div class="page-title">

      <div class="row">
        <div class="col-md-12 col-sm-12 col-xs-12">
          <div class="x_panel">
            <div class="x_title">
              <h2>Create Announcement</h2>
              <div class="clearfix"></div>
            </div>
            <div class="x_content">
              <br />
              <form id="demo-form2" data-parsley-validate class="form-horizontal form-label-left" method="post" action="<?=base_url()?>admin/announceSubmits">
                <div class="form-group">
                  <label class="control-label col-md-3 col-sm-3 col-xs-12" for="title">Title<span class="required">*</span>
                  </label>
                  <div class="col-md-6 col-sm-6 col-xs-12">
                    <input type="text" id="title" name="title" required="required" class="form-control col-md-7 col-xs-12">
                  </div>
                </div>
                <div class="form-group">
                  <label class="control-label col-md-3 col-sm-3 col-xs-12" for="users">Users <span class="required">*</span>
                  </label>
                  <div class="col-md-6 col-sm-6 col-xs-12">
                    <select type="text" id="users" name="users" required="required" class="form-control col-md-7 col-xs-12">
                      <option value="6">Vendor Team Manager</option>
                      <option value="2">Unilever Project Manager</option>
                      <option value="7">Vendor Project Manager</option>
                      <option value="3">Vice President</option>
                      <option value="5">Accountant</option>
                      <option value="8">Project Developers</option>
                    </select>
                  </div>
                </div>
                <div class="x_content">

                  <div class="class"></div>

                  <textarea required name="descr" id="descr" rows="10" cols="10"></textarea>

                  <div class="ln_solid"></div>

                  <div class="form-group">
                    <label class="control-label col-md-3 col-sm-3 col-xs-12">Upload an image <span class="required">*</span>
                    </label>
                    <div class="col-md-6 col-sm-6 col-xs-12">
                      <img id="image-preview" alt="image preview" />
                      <br />
                      <input type="file" name="image-source" id="image-source" onchange="previewImage();" required />
                    </div>
                  </div>
                  <div class="ln_solid"></div>
                  <div class="form-group">
                    <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
                      <button class="btn btn-primary" type="reset">Reset</button>
                      <button type="submit" class="btn btn-success">Submit</button>
                    </div>
                  </div>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
</div>

控制器

public function announceSubmit(){
        $this->session->userdata('user_data');
        $id = $this->session->userdata('user_id');
        $title = $this->input->post('title', true);
        $users = $this->input->post('users', true);
        $content = $this->input->post('descr', true);

        $config['upload_path'] = './uploads/announcements/';
        $config['allowed_types'] = 'jpg|png|jpeg';
        $config['max_size'] = 100;
        $config['max_width'] = 1024;
        $config['max_height'] = 768;

        $this->load->library('upload',$config);

            if(!$this->upload->do_upload('image-source')){
                print_r($this->upload->display_errors());
            }else{

                $announce_image = $this->upload->data('file_name');

                $data = array(
                    'title' => $title,
                    'user_type' => $users,
                    'content' => $content,
                    'image' => $announce_image,
                    'user_id' => $id
                ); 

                $this->Bid_Model->insertItem('announcements', $data);
                redirect('dashboard');
            }
        }

模型

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Bid_Model extends CI_Model {

    function insertItem($table, $data = NULL){
        $this->db->insert($table, $data);
    }

}
?>

1 个答案:

答案 0 :(得分:1)

<form>标记更改为以下标记(添加enctype =“ multipart / form-data”)。

<form id="demo-form2" data-parsley-validate class="form-horizontal form-label-left" method="post" action="<?=base_url()?>admin/announceSubmits" enctype="multipart/form-data">