2012-07-09 68 views
1

我想使用Codegniter的Upload Library上傳圖片,圖片應該上傳到由模型創建並返回的目錄中,每次更改上傳目錄,這是我應該使用的代碼更改配置上傳陣列:在Codeigniter中的控制器中動態設置配置值?

if ($_FILES['image']['size']!=0) { 
       $path = $this->homeModel->createDirectories($id); 
       $this->config->load('upload'); 
       $this->config->set_item('upload', array('upload_path' => $path)); 
       // I've also used 
       // $this->config->set_item('upload_path', $path); 

    if (!$this->upload->do_upload('image')) 
       echo $this->upload->display_errors(); 
       else { 
        $image = $this->upload->data(); 
        $data['image'] = $image['file_name']; 
       }   
      }  

但它似乎並沒有工作。 upload_path不會更改,圖像仍然上傳到我在config/upload.php文件中指定的路徑中。

回答

3

試試這個:

if ($_FILES['image']['size']!=0) { 

    $path = $this->homeModel->createDirectories($id); 
    $this->config->load('upload', TRUE); 
    $config = $this->config->item('upload'); 
    $config['upload_path'] = $path; 
    $this->upload->initialize($config); 

    //etc. 
} 
+0

謝謝你,它的工作:) – Sarah 2012-07-09 10:51:29