无法使用Codeigniter上传文件夹中的图像

时间:2016-08-26 05:47:39

标签: php file codeigniter-3

我正在尝试将图像上传到服务器上的文件夹,并同时在同一个表单中的数据库中存储另一个数据。但它显示错误您没有选择要上传的文件

以下是我的观点和控制器:

查看:

<form role="form" method="post" action="job_insert" enctype="multipart/form-data">
                        <div class="box-body">
                          <div class="form-group">
                              <label>Job Name*</label>
                              <input type="text" class="form-control" name="jname" placeholder="Enter Job Name" required>
                          </div>

                          <div class="form-group">
                            <label>Description</label>
                            <textarea rows="4" cols="10" class="form-control" name="description" required></textarea>
                          </div>
                          <div class="form-group">
                              <label>File input</label>
                            <input type="file" name="userimage">
                          </div>
                          <div class="form-group">
                      <label>Paper Size*</label>

                  <div class="form-group">
                    <label>Paper Cutting Size</label>  
                      <div>
                      <input id="cutting_size" name="cutting_size" type="text" placeholder="Cutting Size" class="form-control input-md" required>  
                    </div>
                  </div>
                  <div class="form-group">
                    <label>Sheet</label>  
                    <div>
                      <input id="sheet" name="sheet" type="text" placeholder="No. of Sheet" class="form-control input-md" required>
                      </div>
                  </div>
                  <div class="form-group">
                    <label>Tin No.</label>  
                    <div>
                      <input id="sheet" name="tin" type="text" placeholder="Tin No." class="form-control input-md" required>
                      </div>
                  </div>
                    </div>
          <!-- /.box-body -->
                    <div style="margin-left: 600px" class="box-footer">
                      <button type="submit" class="btn btn-primary">Submit</button>
                    </div>
                </form>

控制器:

public function job_insert()
        {
            $this->form_validation->set_rules('jname', 'job_name', 'trim');
            if($this->form_validation->run()== FALSE)
            {
                $this->load->view('invalid_jobname');
            }
            else
            {
                $this->do_upload();

                $data = $_SESSION['email'];
                $id = $this->user_model->get_client_id($data);
                $data = array('cid' => $id,
                                'job_name' => $this->input->post("jname"),
                                'job_id' => $this->input->post("job_id"),
                                'description' => $this->input->post("description"),
                                'img_name' => $img_name,
                                'adate' => Date('Y-m-d'),
                                'status' => 'NEW',
                                'paper_size' => $this->input->post("paper_size"),
                                'paper_type' => $this->input->post("paper_type"),
                                'cutting_size'=> $this->input->post("cutting_size"),
                                'sheet'=> $this->input->post("sheet"),
                                'tin' => $this->input->post("tin"),
                                'lamination' => $this->input->post("checkboxes"),
                                'print_type' => $this->input->post("checkboxes1"),
                                'binding' => $this->input->post("checkboxes2"),
                                 );

                $query = $this->user_model->job_insert($data);
                if ($query == TRUE) 
                {
                    $this->load->view('job_submitted');
                }
                else
                {
                    $this->load->view('error_page');
                }

            }
        }

这是我的do_upload函数:

public function do_upload()
    {
    $config = array(
                'upload_path' => "./uploads/",
                'allowed_types' => "gif|jpg|png|jpeg|pdf",
                'overwrite' => FALSE,
                'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
                'max_height' => "768",
                'max_width' => "1024"
                 );

                $this->load->library('upload', $config);
                $image = $this->input->post("image");
                echo $image;
                if(!$this->upload->do_upload('userimage'))
                {   
                    $error = $this->upload->display_errors();
                    echo $error;
                }
        }

3 个答案:

答案 0 :(得分:1)

将此更改为

<input type="file" name="image">

<input type="file" name="userfile"/>

和do_upload

if(!$this->upload->do_upload())

要      if(!$ this-&gt; upload-&gt; do_upload(&#39; userfile&#39;))

答案 1 :(得分:0)

在表单标记中提供 enctype =“multipart / form-data”。没有它你就无法上传图片。

答案 2 :(得分:0)

使用以下代码更新表单标记

<form role="form" method="post" action="job_insert" enctype="multipart/form-data">