【问题标题】:codeigniter upload file $_FILES returns empty array array(0) { }codeigniter 上传文件 $_FILES 返回空数组 array(0) { }
【发布时间】:2015-08-18 19:33:34
【问题描述】:

我有一个上传图片的功能,可以很好地将图片名称插入数据库。我创建了一个名为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) 时,它返回一个空数组,这意味着没有选择文件。

我该如何解决这个问题?

【问题讨论】:

  • 你关闭表单标签了吗???
  • 是的,我有。该表单正在工作,因为我在另一个上传情况下使用了确切的表单代码。这是我的控制器的逻辑问题。它没有读取用户在表单中选择的文件。

标签: php forms file codeigniter upload


【解决方案1】:
                    $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 --->
                   }

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

【讨论】:

    【解决方案2】:

    检查您是否加载了 CodeIgniter 表单助手。 $this->load->helper('form') 在表单控制器之前。或者你可以使用 application/config/autoload.php 自动加载它,方法是把这个 $autoload['helper'] = array('form');希望它会起作用。

    【讨论】:

    • 你的表单链接 提交给resetPasswordController控制器中的news函数。但是您正在处理公共函数编辑()。数据是否提交到您想要的正确目的地?
    • 抱歉,我复制到文本时忘记更改了。它将进入正确的功能。
    猜你喜欢
    • 2014-10-11
    • 2015-01-01
    • 1970-01-01
    • 2011-11-15
    • 2021-02-26
    • 2015-02-04
    • 1970-01-01
    • 2014-01-20
    • 2013-05-09
    相关资源
    最近更新 更多