表单未在Codeigniter 2.0中上传文件

时间:2019-03-07 09:13:34

标签: php codeigniter-2

我尝试了所有可能的方法,但没有成功。只是尝试使用代码点火器上传文件,但无法解决我收到的错误

<pre>Array
(
 [error] => <p>You did not select a file to upload.</p>
)

我在本地主机上的普通核心php中尝试了正常,但不能与代码点火器一起使用。它只是不选择文件。如果我用var_dump($_FILES['fileToUpload']);进行检查,结果将是array(0)

表格代码

<form id="contact_form" enctype="multipart/form-data" method="post" action="<?php echo base_url();?>Main/do_upload">
 <input type="file" class="form-control" name="fileToUpload" id="fileToUpload">
</form>

控制器代码

$config = array(
    'upload_path' => "./uploads/",
    'allowed_types' => "gif|jpg|png|jpeg|pdf",
    'overwrite' => TRUE,
    '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);

if($this->upload->do_upload())
{
    $data = array('upload_data' => $this->upload->data());
    echo "<pre>";
    var_dump($data);
    // $this->load->view('upload_success',$data);
}else{
    $error = array('error' => $this->upload->display_errors()); 
    echo "<pre>";
    print_r($error);
}

配置

$autoload['libraries'] = array("session", "email", "database");
$autoload['helper'] = array("url", "file", "form");

有什么我不知道的吗?请指导我被困在这里。

2 个答案:

答案 0 :(得分:1)

您错过了do_upload()中的输入文件名:

使用:

if(!$this->upload->do_upload('image_file'))
{
   //$this->upload->display_errors()
}
else
{
   //$this->upload->data()
}

代替:

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

答案 1 :(得分:0)

您错过了$this->upload->do_upload中的参数,请检查下面的代码。

public function do_upload(){
        $config = array(
            'upload_path' => "assets/uploads/",
            'allowed_types' => "gif|jpg|png|jpeg|pdf",
            'overwrite' => TRUE,
            '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);
        $this->upload->initialize($config);

        if($this->upload->do_upload('fileToUpload'))
        {
            $data = array('upload_data' => $this->upload->data());
            echo "<pre>";
            var_dump($data);
            // $this->load->view('upload_success',$data);
        }else{
            $error = array('error' => $this->upload->display_errors()); 
            echo "<pre>";
            print_r($error);
        }
    }

$this->upload->do_upload('fileToUpload')中传递文件上传名称