一切都很好,但是当我打开上传文件夹时,没有任何图像。
这是控制器“admin_news”。我尝试在localhost / site / asset / upload上传图像文件。但是当我点击提交时,只有信息(标题,新闻和类别)进入数据库,但文件没有上传。
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'html', 'file'));
$config['upload_path'] = base_url('asset/upload/');
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
}
public function add_news(){
$this->load->helper('form');
$this->load->model('AddNews');
$this->load->view('templates/header', $data);
$this->load->view('admin/add_news');
$this->load->view('templates/footer');
if($this->input->post('submit')){
if ($this->upload->do_upload('image'))
{
$this->upload->initialize($config);
$this->upload->data();
}
$this->AddNews->entry_insert();
redirect("admin_news/index");
}
}
视图只有:
<?php echo form_open_multipart(''); ?>
<input type="text" name="title" value="Title..." onfocus="this.value=''" /><br />
<input type="file" name="image" /><br />
...
答案 0 :(得分:1)
这不应该是网址:
$config['upload_path'] = base_url('asset/upload/');
它应该是服务器某处的路径,可以是完整的绝对路径,也可以是相对的路径(Codeigniter中的所有路径都与index.php相关)。
使用以下任何一种:
// Full path
$config['upload_path'] = FCPATH.'asset/upload/';
// Relative
$config['upload_path'] = './asset/upload/';
还有一件事:
if ($this->upload->do_upload('image'))
{
// You don't need this, and besides $config is undefined
// $this->upload->initialize($config);
// You don't seem to be doing anything with this?
$this->upload->data();
// Move this here in case upload fails
$this->AddNews->entry_insert();
redirect("admin_news/index");
}
// Make sure to show errors
else
{
echo $this->upload->display_errors();
}
答案 1 :(得分:0)
我认为在你的代码中,问题是你在do_upload方法之后声明了配置
if ($this->upload->do_upload('image'))
{
$this->upload->initialize($config);
$this->upload->data();
}
在使用该方法之前,应该初始化配置。我认为这就是问题所在。所以你必须在do_upload
方法之前进行配置