使用代码点火器上传时"did not select a file to upload"



我试图上传图像,但它总是给我"您没有选择要上传的文件"。

我的控制器

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);
    }}

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

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

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

应该是:

$image1='image';
  • 视图页面

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

    控制器

和控制器上传代码应为$this->upload->do_upload("field_name")
和jsut检查文件是否到达服务器端通过打印像

print_r($_FILES);

如果你得到null数组,然后确保客户端代码是正确的。

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

$image1=$this->input->post('image');
之前,是

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

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

确保使用

form_open_multipart ()

这是一个再熟悉不过的问题了。

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

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

首先,参考Nana Partykar的评论,"在你的控制器中,i'm not able to see any is_uploaded_file() function ?"这个评论告诉我们,人们误解了两个名字相似但不同的文件。我知道,因为有一段时间我认为他们一定是指同一个文件,控制器文件(名为"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给了我们一个for循环,循环遍历$_FILES['userfile']数组。假设你上传了三张照片。每个$_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=文件的名称。如果你没有告诉它表单输入的名称,它将默认为$_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);
    }

最新更新