我在模型中使用了以下函数:
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>
如何修复错误?
4条答案
按热度按时间ybzsozfc1#
请更新此行,
ldfqzlk82#
您需要给予基目录而不是
base_url()
。因此,将代码更改为:nwlls2ji3#
您可以使用
FCPATH
:更多信息:https://www.codeigniter.com/user_guide/general/reserved_names.html
2izufjch4#
FCPATH:index.php所在的前端控制器路径或根文件夹
APPPATH:****应用程序文件夹
在root文件夹下创建uploads文件夹:
在application文件夹下创建uploads文件夹:
如果:如果您在root文件夹之外创建了uploads文件夹: