2015-11-19 69 views
0

我想做一個上傳表單,但是當我提交它,它只是刷新表單,甚至不會回到指定的路線,並沒有消息顯示。這裏是我的add()功能:CodeIgniter 3上傳圖像只是重新加載頁面

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

形式:

<form action="/admin/banners/adicionar" method="POST" enctype="multipart/form-data"> 
    <div class="form-group"> 
     <small>Imagens de até 1024x768</small> 
    </div> 
    <div class="form-group"> 
     <label for="selecionar_imagem">Selecionar Imagem</label> 
     <input type="file" name="imagem" required/> 
    </div> 
    <hr/> 
    <div class="form-group"> 
     <button class="btn btn-primary pull-right">Adicionar</button> 
    </div> 
</form> 

我缺少的東西?

+0

哪裏是'adicionar()'? –

+1

窗體動作似乎是調用adicionar()而不是add() –

+0

@Ness這是我的路由名稱。我在其他頁面使用相同的模式,並且它工作得很好。然後路由調用控制器'banner'和方法'add'。這只是一個友好的pt_BR URI。 – mfgabriel92

回答

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