文件上传codeIgniter中的错误

时间:2018-07-24 12:28:46

标签: php codeigniter codeigniter-3 codeigniter-upload

下面是我的控制器的代码。...

public function do_upload()
    {

        $config['upload_path']='./upload/';
        $config['allowed_types']='gif|jpg|png';
        $this->load->library('upload',$config);
        $this->upload->do_upload('image_file');
        if($this->upload->do_upload('image_file'))
        {
            $filedata = $this->upload->data();
            $filename = $filedata['raw_name'].$filedata['file_ext'];

            return $filename;
        }

    }

调用此函数后,您要在控制器中进行此上传...

    if($_FILES)
                    {
                        $this->do_upload();
                    }

但文件未上传....为什么?

2 个答案:

答案 0 :(得分:0)

希望这对您有帮助:

您的do_upload方法应如下所示:

public function do_upload() 
{
    $config['upload_path'] = './upload/';
    $config['allowed_types']='gif|jpg|png';
    $this->load->library('upload', $config);
    if($this->upload->do_upload('image_file'))
    {
        $filename = $this->upload->data('file_name');
        echo  $filename;die;
    }
    else
    {
        print_r($this->upload->display_errors());
        die;
    }
}

更新

set upload_max_filesize在您的php ini中大于2MB,

如果它是wamp,请遵循:

click on wamp => php => php settings => upload_max_filesize

答案 1 :(得分:0)

我认为您错过了这一行。

$ config ['upload_path'] ='。/ upload /';

$ config ['allowed_types'] ='gif | jpg | png';


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

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

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

    $filename = $this->upload->data('file_name');
    echo  $filename;die;
}
else
{
    print_r($this->upload->display_errors());
    die;
}