Codeigniter:上传路径似乎无效

时间:2018-03-17 04:38:41

标签: php codeigniter

我在我的模型中使用以下功能:

function uploadsinglepicture($uploadpath){

    $config['upload_path'] =$uploadpath;
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '';
    $config['max_width']  = '';
    $config['max_height']  = '';
    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload('userfile'))
    {
        $error = array('error' => $this->upload->display_errors());
        print_r($error);
        if($this->input->post('id') == ''){
            $insertion['image'] = '';
        }
        //$this->load->view('upload_form', $error);
    }
    else{
        $data = array('upload_data' => $this->upload->data());
        $insertion['image'] = $data['upload_data']['file_name'];
    }

    $image = $insertion['image'];
    return $image;
}

这是我访问控制器中的函数的方法:

if(!empty($this->input->post())){
                $path= base_url().'assets/front/img';
                $this->general->uploadsinglepicture($path);
                redirect(base_url().'admin/home/index/sliderupated');
}

但我得到的错误是:

  

数组([错误] =>上传路径似乎无效。   )

如果我打印$path,这就是我得到的

http://localhost/site/assets/front/img/

并在浏览器中以真实路径打开。我在视图中的代码如下

<form  method="post" enctype="multipart/form-data" action ="<?=base_url()?>admin/home/index" >
    <label>Upload Picture </label>
    <input type='file'  name='userfile' />
    <input type="hidden" name="updateimage">
    <input type="submit" class="btn btn-primary pull-right" />
</form>

如何解决错误?

3 个答案:

答案 0 :(得分:5)

请更新此行,

from ,  $path= base_url().'assets/front/img'; 

to,     $path= FCPATH.'assets/front/img';

答案 1 :(得分:2)

您需要提供基本目录,而不是给base_url()。所以将代码更改为:

if(!empty($this->input->post())){
                $path= FCPATH.'assets/front/img';
                $this->general->uploadsinglepicture($path);
                redirect(base_url().'admin/home/index/sliderupated');
            }

答案 2 :(得分:1)

您可以使用FCPATH这个:

  $path= FCPATH.'assets/front/img';

了解更多:https://www.codeigniter.com/user_guide/general/reserved_names.html