codeigniter 代码触发器:上载路径似乎无效

q43xntqr  于 2022-12-07  发布在  其他
关注(0)|答案(4)|浏览(131)

我在模型中使用了以下函数:

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

但我得到的错误是:
Array([error] =〉上传路径似乎无效。)
如果输出$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>

如何修复错误?

ybzsozfc

ybzsozfc1#

请更新此行,

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

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

ldfqzlk82#

您需要给予基目录而不是base_url()。因此,将代码更改为:

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

nwlls2ji3#

您可以使用FCPATH

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

更多信息:https://www.codeigniter.com/user_guide/general/reserved_names.html

2izufjch

2izufjch4#

FCPATH:index.php所在的前端控制器路径或文件夹
APPPATH:****应用程序文件夹

root文件夹下创建uploads文件夹:

$config['upload_path'] = realpath(FCPATH.'uploads');

application文件夹下创建uploads文件夹:

$config['upload_path'] = realpath(APPPATH.'uploads');

如果:如果您在root文件夹之外创建了uploads文件夹:

$config['upload_path'] = realpath($_SERVER['DOCUMENT_ROOT'].'/uploads');

相关问题