2014-02-27 70 views
1

我有我多上傳與笨問題,笨上傳多個文件的路徑不同的數據庫

  1. 使用圖像
  2. 另一個使用PDF

當我上傳的文件上傳兩次,如何調用不同的路徑上傳到數據庫。這是我的代碼

控制器

public function upload(){ 

    $catalog='catalog'; 
    $userfile='userfile'; 

    //for cover 
    $config['upload_path'] = './file/book/'; //Use relative or absolute path 
    $config['allowed_types'] = 'gif|jpg|png|'; 
    $config['max_size'] = '100000'; 
    $config['max_width'] = '1024'; 
    $config['max_height'] = '768'; 
    $this->load->library('upload', $config); 
    $this->upload->initialize($config); 

    //$c=$this->upload->do_upload($userfile); 

    //for catalog 
    $config['upload_path'] = './file/book/pdf/'; //Use relative or absolute path 
    $config['allowed_types'] = 'pdf'; 
    $config['max_size'] = '1000000'; 
    $this->load->library('upload', $config); 
    $this->upload->initialize($config); 

    //$cat=$this->upload->do_upload($catalog); 


    if(!$this->upload->do_upload($catalog) &&   $this->upload->do_upload($userfile)){ 


     $error = array('error' => $this->upload->display_errors()); 
     $this->load->view('uploadds', $error); 

    }else{ 



     $this->load->model("book_model"); 
     $this->book_model->addnewbook(); 
     redirect('book/book');  
    } 

}

這種模式

function addnewbook(){ 
    $fcat=array('upload_data' => $this->upload->data($userfile)); 
    $fcatalog=array('upload_dataa' => $this->upload->data($catalog)); 
} 
+0

快速谷歌搜索給了我這個。 http://pastebin.com/g9J6RxW1。還沒有嘗試過。會嘗試讓你知道。 – nightgaunt

回答

4

你需要獨立處理多個上傳。爲此,在加載上傳庫時,您必須創建單獨的自定義對象以供上傳。 (見代碼註釋)

public function upload() { 

    // Cover upload 
    $config = array(); 
    $config['upload_path'] = './file/book/'; 
    $config['allowed_types'] = 'gif|jpg|png|'; 
    $config['max_size'] = '100000'; 
    $config['max_width'] = '1024'; 
    $config['max_height'] = '768'; 
    $this->load->library('upload', $config, 'coverupload'); // Create custom object for cover upload 
    $this->coverupload->initialize($config); 
    $upload_cover = $this->coverupload->do_upload('cover'); 

    // Catalog upload 
    $config = array(); 
    $config['upload_path'] = './file/book/pdf/'; 
    $config['allowed_types'] = 'pdf'; 
    $config['max_size'] = '1000000'; 
    $this->load->library('upload', $config, 'catalogupload'); // Create custom object for catalog upload 
    $this->catalogupload->initialize($config); 
    $upload_catalog = $this->catalogupload->do_upload('catalog'); 

    // Check uploads success 
    if ($upload_cover && $upload_catalog) { 

     // Both Upload Success 

     // Data of your cover file 
     $cover_data = $this->coverupload->data(); 
     print_r($cover_data); 

     // Data of your catalog file 
     $catlog_data = $this->catalogupload->data();   
     print_r($catlog_data); 
    } else { 

     // Error Occured in one of the uploads 

     echo 'Cover upload Error : ' . $this->coverupload->display_errors() . '<br/>'; 
     echo 'Catlog upload Error : ' . $this->catalogupload->display_errors() . '<br/>'; 
    } 
    } 

使用上$cover_data['full_path']$catlog_data['full_path']數據來更新數據庫

+0

你不需要多次加載庫,你可以調用初始化在同一個庫,它會切換配置出... –

+0

謝謝迪諾,完美 –