图片上传codeigniter



一切都很好,但是当我打开上传文件夹时没有任何图像。

这里是控制器"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 />

不应该是URL:

$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();
}

我认为在你的代码中,问题是你在do_upload方法后声明了配置

if ($this->upload->do_upload('image'))
        {
            $this->upload->initialize($config);
            $this->upload->data();
        }
在使用该方法之前,应该初始化

配置。我想这就是问题所在。所以你必须在do_upload方法之前做配置

相关内容

  • 没有找到相关文章

最新更新