【问题标题】:"did not select a file to upload" when uploading using codeigniter使用 codeigniter 上传时“没有选择要上传的文件”
【发布时间】:2011-09-17 19:10:41
【问题描述】:

我试图上传一张图片,但它总是给我“你没有选择要上传的文件”。

我的控制器

function add()
{

        $thedate=date('Y/n/j h:i:s');
        $replace = array(":"," ","/");
        $newname=str_ireplace($replace, "-", $thedate);

        $config['upload_path'] = './upload/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['file_name']=$newname;
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

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

        $this->form_validation->set_rules('title','title','trim|required');
        $this->form_validation->set_rules('description','Description','trim|required');
        $image1=$this->input->post('image');


     if ($this->form_validation->run()==FALSE){

            $this->addview();   

            return false;

        }


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

        $error = array('error' => $this->upload->display_errors());
        $this->load->view('upload_error', $error);


         }

       else {
        $mage=$this->upload->do_upload($image1);

            $data =array(
            'title'=>$this->input->post('title'),
            'descrip'=>$this->input->post('description'),

            'image' => $mage['file_name']

    );  


            $this->load->model('member_functions');

            $q=$this->member_functions->insert($data);
    }}

所有文件要求和文件权限都已设置,但我仍然得到那个错误。有人可以告诉我我做错了什么

【问题讨论】:

    标签: php codeigniter file-upload


    【解决方案1】:

    $this->upload->do_upload() 函数的参数应该是表单字段的名称。 (如果你在没有参数的情况下调用它 userfile 将被使用)。在您的情况下,它似乎应该是“图像”。而不是:

    $image1=$this->input->post('image');
    

    应该是:

    $image1='image';
    

    【讨论】:

    • 非常感谢...但现在图像值没有通过 file_name。我得到“列'图像'不能为空”。 "'image' => $mage['file_name']" 是传递文件名的正确方式吗?
    • 不,我认为“图像”是表单元素的名称。您应该将表单元素命名为:<input type="file" name="image" /> 或者如果您想要 do_upload 的默认值 <input type="file" name="userfile" />
    【解决方案2】:
    • 查看页面

    表单标签应包含enctype="multipart/form-data"
    IE, <form action='' method=''enctype="multipart/form-data> <input type='file' name='field_name'/>

    • 控制器

    并且在控制器中上传代码应该是$this-&gt;upload-&gt;do_upload("field_name")
    并且jsut通过像打印一样检查文件是否到达服务器端

    print_r($_FILES);
    

    如果你得到空数组,那么确保客户端代码是正确的。

    【讨论】:

      【解决方案3】:

      我完全同意关于确保表单中文件元素的名称是 userfile 或者如果不同,则传递给 do_upload 方法的建议。但是,为了将来参考,还值得注意这一行:

      $image1=$this->input->post('image');
      

      来之前

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

      我发现input 类的post 方法在调用do_upload 方法之前不会返回任何内容。此外,您已经在if 语句中调用了它,因此您无需在else 子句中再次调用它。调用do_upload 后,您现在可以使用$this-&gt;input-&gt;post 访问表单元素,并使用$this-&gt;upload-&gt;data() 访问有关上传文件的信息

      【讨论】:

        【解决方案4】:

        确保你使用

        form_open_multipart()

        如果您正在使用表单助手。

        【讨论】:

          【解决方案5】:

          这个问题太熟悉了。

          我将把我的答案作为更完整的解释添加到我找到的另一个答案 here。我提交的代码不是不同的代码,而是添加了一些他的答案中没有提到的行和细节。

          尽管看起来他的答案在技术上是正确的,也许他上面的答案在他们自己的方式上也是正确的,但它们对我不起作用。我必须研究这个问题以找出真正发生的事情,并且我对这个过程了解得足够多,以了解如何编写自己的解决方案。

          首先,参考 Nana Partykar 的评论,“在你的控制器中,我看不到任何 is_uploaded_file() 函数?”该评论告诉我们,人们误解了名称相似但不同的两个文件。我知道,因为有一段时间我认为它们必须引用同一个文件,即控制器文件(名为“Uploader.php”)。我可以看到几乎所有这些问题都引用了相同的“如何使用 Ajax 上传多个文件”教程,包括我自己的版本。我们都使用的代码完全一样。

          但是,控制器文件是“Uploader.php”。在你看到 $this->upload->do_upload() 或 $this->upload->do_upload('userfile') 甚至 $this->upload->do_upload('files') 的地方,这是指系统/名为“Upload.php”的库模块文件。请注意,在调用 do_upload() 函数之前,您必须调用此行: $this->load->library('upload', $config);

          Sachin Marwha 为我们提供了一个遍历 $_FILES['userfile'] 数组的 for 循环。假设您上传了三张图片。每个 $_FILES['userfile'] 元素本身由 5 个“属性”组成:名称、类型、tmp_name、错误、大小。您可以在PHP 上看到这些 $_FILE 属性。

          您一次只想将一个文件传递给 do_upload()。您不想一次将所有三个(甚至 20 个)文件传递给 do_upload。这意味着您必须在调用 do_upload() 之前将 $_FILES['userfile'] 数组分解为单独的文件。为此,我创建了 $_FILES 数组的 $_FILES['f'] 元素。我通过在 system/library/Upload.php 文件中的 do_upload($file = 'userfile') 函数中设置断点来解决这个问题,看看我在哪里得到了臭名昭著的“没有选择要上传的文件” (包括我自己)一直在抱怨。您会发现,该函数使用表单发送到控制器的原始 $_FILES 数组。但它实际上只使用表单中输入 type=file 的名称。如果您不告诉它表单输入的名称,它将默认为 $_FILES['userfile']。事实证明,这是我最大的问题,因为如果我使用输入字段的名称,那么该字段会传递一个数组或文件集合,而不仅仅是单个文件。所以我必须制作一个特殊的 $_FILES['f] 元素,并且只传递 $_FILES['f']。

          这就是我的做法,相信我,我尝试了此页面上的所有版本和其他版本,不仅仅是一个 StackOverflow,还包括其他教程:

              $cpt = count($_FILES['userfile']['name']);
              for($i=0; $i < $cpt; $i++)
              {
                  unset($config);
                  $config = array();
                  $config['upload_path']   = $path;
                  $config['allowed_types'] = 'gif|jpg|png';
                  $config['max_size'] = '1000';
                  $config['overwrite'] = TRUE;
                  $config['remove_spaces'] = FALSE;
                  $config['file_name'] = $_FILES['userfile']['name'][$i];
          
                  // Create a new 'f' element of the $_FILES object, and assign the name, type, tmp_name, error, and size properties to the corresponding 'userfile' of this iteration of the FOR loop.
                  $_FILES['f']['name'] =  $_FILES['userfile']['name'][$i];
                  $_FILES['f']['type'] = $_FILES['userfile']['type'][$i];
                  $_FILES['f']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
                  $_FILES['f']['error'] = $_FILES['userfile']['error'][$i];
                  $_FILES['f']['size'] = $_FILES['userfile']['size'][$i];
          
                  $this->load->library('upload', $config);
                  $this->upload->initialize($config);            
                  if (! $this->upload->do_upload('f'))
                  {
                      $data['errors'] = $this->upload->display_errors();
                  }
                  else
                  {
                      $data['errors'] = "SUCCESS";
                  }
          
                  unset($config);
                  $config = array();
                  $config['image_library']    = 'gd2';
                  $config['source_image']     = $path . $_FILES['userfile']['name'][$i];
                  $config['create_thumb']     = TRUE;
                  $config['maintain_ratio']   = TRUE;
                  $config['thumb_marker']     = '.thumb';
                  $config['width']            = 100;
                  $config['height']           = 100;
          
                  $this->load->library('image_lib', $config);
                  $this->image_lib->clear();
                  $this->image_lib->initialize($config);
                  $this->image_lib->resize();
                  $types = array('.jpg');
              }        
          

          它在 for i 循环中取消设置 $config 数组,然后重新制作 $config 数组,这是为每个图片文件制作缩略图的部分。

          完整的控制器上传功能:

              public function upload_asset_photo()
              {
                  $data = array();
                  $dateArray = explode("/",$this->input->post('date'));
                  $date = $dateArray[2] . "/" . $dateArray[0] . "/" . $dateArray[1]; // year/month/day
                  $cid = $this->config->item('cid'); // this is a special company id I use, unnecessary to you guys.
                  $padded_as_id = sprintf("%010d", $this->uri->segment(3)); // this makes an "asset id" like "3" into "0000000003"
                  $path = 'properties_/' . $padded_as_id . '/' . $date . '/'; // file path
                  if (!is_dir($path)) {
                      mkdir($path,0755,true); //makes the ile path, if it doesn't exist
                  }
          
                  $cpt = count($_FILES['userfile']['name']);
                  for($i=0; $i < $cpt; $i++)
                  {
                      unset($config);
                      $config = array();
                      $config['upload_path']   = $path;
                      $config['allowed_types'] = 'gif|jpg|png';
                      $config['max_size'] = '1000';
                      $config['overwrite'] = TRUE;
                      $config['remove_spaces'] = FALSE;
                      $config['file_name'] = $_FILES['userfile']['name'][$i];
          
                      $_FILES['f']['name'] =  $_FILES['userfile']['name'][$i];
                      $_FILES['f']['type'] = $_FILES['userfile']['type'][$i];
                      $_FILES['f']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
                      $_FILES['f']['error'] = $_FILES['userfile']['error'][$i];
                      $_FILES['f']['size'] = $_FILES['userfile']['size'][$i];
          
                      $this->load->library('upload', $config);
                      $this->upload->initialize($config);            
                      if (! $this->upload->do_upload('f'))
                      {
                          $data['errors'] = $this->upload->display_errors();
                      }
                      else
                      {
                          $data['errors'] = "SUCCESS";
                      }
          
                      unset($config);
                      $config = array();
                      $config['image_library']    = 'gd2';
                      $config['source_image']     = $path . $_FILES['userfile']['name'][$i];
                      $config['create_thumb']     = TRUE;
                      $config['maintain_ratio']   = TRUE;
                      $config['thumb_marker']     = '.thumb';
                      $config['width']            = 100;
                      $config['height']           = 100;
          
                      $this->load->library('image_lib', $config);
                      $this->image_lib->clear();
                      $this->image_lib->initialize($config);
                      $this->image_lib->resize();
                      $types = array('.jpg');
                  }        
          
                  header('Content-Type: application/json');
                  echo json_encode($data);
              }
          

          【讨论】:

            猜你喜欢
            • 2014-03-05
            • 2018-03-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-03-13
            • 2012-07-05
            • 2015-11-20
            相关资源
            最近更新 更多