codeigniter上传文件$ _FILES返回空数组数组(0){}

时间:2015-06-04 09:08:28

标签: php forms file codeigniter upload

我有一个上传图像的功能,可以将图像名称完全插入到数据库中。我创建了一个名为edit()的新功能,其代码完全相同,允许用户编辑表单并上传图像。此映像名称将更新数据库中的现有映像名称。

但是,每次用户编辑表单并选择新图像时,它都会擦除数据库中的先前名称而不显示任何内容,而不是保存新图像名称。

我的控制员:

  public function edit(){     

    $this->form_validation->set_rules('title', 'Title', 'required|xss_clean|trim|is_unique[news.title]');
    $this->form_validation->set_rules('description', 'description', 'required|xss_clean|trim');
    $this->form_validation->set_rules('notes', 'notes', 'required|xss_clean|trim');


      if ($this->form_validation->run() == FALSE)
      {
                $data['error'] =  '';
            $data['page_title']="Edit News";
                $this->load->view("edit_news_form",$data);

           }
          else {

               $new = array(
               'upload_path' => "./images/news",
               'allowed_types' => "gif|jpg|png|jpeg|JPG",
               'overwrite' =>False,          
               'max_height' => "768",
               'max_width' => "1024"           
               );

                  $this->load->library('upload', $new);
                  $this->load->helper(array('form', 'url'));

           if(isset($_FILES)) {       

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

                   $data = array('upload_data' => $this->upload>data());
                         $this->load->view('manage_news',$data);
                      }
                      else {
              if(empty($_FILES['userfile']['name'])){ $imagename ='';}

                   else {    

                         $message2 = "Please choose another file type. gif,jpg,png,jpeg ";
                         echo "<script type='text/javascript'>alert('$message2');</script>";

                         redirect('resetPasswordController/edit_news_form', 'refresh');

                       }                                  
              }
              }

            $this->db->trans_start();         


        $imagename = $this->upload->data();

        $data = array(
            'image'=>$imagename['file_name'],      
            );
        $this->users_model->update($id,$data);
        $this->db->trans_complete();

          $message2 = "The selected news has been edited";
         echo "<script type='text/javascript'>alert('$message2');</script>";


          redirect('resetPasswordController/manage_news', 'refresh');

    }

   }

我的观点:

 <?php echo form_open_multipart('resetPasswordController/news');?>
 <label>Image</label>
 <input name = "userfile" type="file" />
 <label> <input type="submit" name="submit" value="Save and Continue">
 </label>  

我的模特:

    function update($id,$data){
    $this->db->where('id', $id);
    $this->db->update('news',$data);
    }

我的模型函数正在工作,因为它正在将数据库更新为空。我认为问题出在视图和控制器上。

当我键入var_dump($_FILES)时,它返回一个空数组,这意味着没有选择任何文件。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

                    $this->load->library('upload');
                    $config = array(
                                'upload_path'   => './uploads/',
                                'allowed_types' => 'gif|jpg|png|pdf|doc|docx',
                                'max_size'      => 10000,
                                'overwrite'     => FALSE
                        );

                    $files = $_FILES;
                    $cpt = count($_FILES['file']['name']);

                   for($i=0; $i<$cpt; $i++)
                   { 

                        $_FILES['userfile']['name']     = $files['file']['name'][$i];
                        $_FILES['userfile']['type']     = $files['file']['type'][$i];
                        $_FILES['userfile']['tmp_name'] = $files['file']['tmp_name'][$i];
                        $_FILES['userfile']['error']    = $files['file']['error'][$i];
                        $_FILES['userfile']['size']     = $files['file']['size'][$i];    

                        $this->upload->initialize($config);
                        $this->upload->do_upload();

                        <!-- INSERT DB HERE --->
                   }

您可以将此作为参考,它对我有用。

答案 1 :(得分:-1)

检查您是否加载了CodeIgniter表单助手。在表单控制器之前,$ this-&gt; load-&gt; helper('form')。或者你可以使用application / config / autoload.php自动加载它来放置这个$ autoload ['helper'] = array('form');希望它能奏效。