CodeIgniter 3上传图片只需重新加载页面

时间:2015-11-19 16:32:12

标签: codeigniter image-uploading

我正在尝试制作一个上传表单,但是当我提交它时,它只是刷新表单,甚至没有返回到指定的路由,也没有显示任何消息。这是我的Route::group(array('domain' => '{subdomain}.mysite.com'), function() { Route::get('customer/{customerId}', function() { //blablabla; }); }); 函数:

add()

表格:

public function add()
{
    if ($this->input->post()) {
        $data['upload_path'] = './uploads/';
        $data['allowed_types'] = 'jpg|png|gif';
        $data['max_size'] = '500';
        $data['max_width'] = '1024';
        $data['max_height'] = '768';

        $this->load->library('upload', $data);

        if ($this->upload->do_upload('imagem')) {
            $image_data = $this->upload->data();
            $this->session->set_flashdata('message', 'Imagem \'' . $image_data['file_name'] . '\' adicionada');
        } else {
            $this->session->set_flashdata('message', 'Erro: ' . $this->upload->display_errors());
        }

        redirect('/admin/banners', 'refresh');
    }

    $data['page'] = $this->config->item('admin_template_dir_admin') . '/banners/add';
    $data['module'] = 'admin';

    $this->load->view($this->_container, $data);
}

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

我做到了。我似乎必须修改我做事情的方式。我必须创建一个单独的方法upload(),而不是在渲染视图的同一方法中执行if条件。

public function add()
{
    $data['page'] = $this->config->item('admin_template_dir_admin') . '/banners/add';
    $data['module'] = 'admin';

    $this->load->view($this->_container, $data);
}

public function upload()
{
    $this->load->library('upload', array(
        'upload_path' => './uploads/',
        'allowed_types' => 'jpg',
        'max_size' => '5000',
        'overwrite' => true
    ));

    if ($this->upload->do_upload('imagem')) {
        $this->session->set_flashdata('message', 'Imagem de Banner adicionada');
    } else {
        $this->session->set_flashdata('message', 'Erro: ' . $this->upload->display_errors());
    }

    redirect('/admin/banners', 'refresh');
}

然后在form中,将method修改为/admin/banners/upload