2010-06-25 91 views
2

我正在使用Codeigniter,我試圖將多個文件上傳到數據庫中。 這些文件是獨立的字段,而不是一個字段的多個文件。Codeigniter - 多文件上傳 - >插入數據庫 - 有問題!

我跟着這個帖子 - http://codeigniter.com/forums/viewthread/110130/P0/

我幾乎都有它的工作,但我有一些麻煩添加數據上傳到我的分貝。

這裏是我的控制器功能...

function addStation(){ 
     // set common properties 
     $data['title'] = 'Add new station'; 
     $data['action'] = site_url('admin/station/addStation'); 
     $data['link_back'] = anchor('admin/station/index/','Back to list of stations',array('class'=>'back')); 

     // set validation properties 
     $this->_set_fields(); 
     $this->_set_rules(); 

     $config['allowed_types'] = 'pdf|xls|doc|docx'; 
    $config['max_size'] = '1000'; 
    $config['max_width'] = '1024'; 
    $config['max_height'] = '768'; 
    $path_to_uploads='./uploads/files/stations'; 
    $config['upload_path'] = $path_to_uploads; 

    // File specific (overrule global and optional) 
$config['logo_file']['upload_path'] = './uploads/images/stations'; 
$config['logo_file']['allowed_types'] = 'gif|jpg|png'; 


    $this->load->library('upload', $config); 
    //add this 
    $this->upload->initialize($config); 
if (!$this->upload->do_upload(array('rajar_file','playlist_file','map_file', 'logo_file'))){ 
     $error = $this->upload->display_errors(); 
     echo "<script>alert($error);</script>"; 
    }else{ 
     $data = array('upload_data' => $this->upload->data(array('rajar_file','playlist_file','map_file', 'logo_file'))); 

     $rajar_file_name = $upload_data['rajar_file']['file_name']; 
     $rajar_full_file_path = $path_to_uploads.'/'.$rajar_file_name; 

     $playlist_file_name = $upload_data['playlist_file']['file_name']; 
     $playlist_full_file_path = $path_to_uploads.'/'.$playlist_file_name; 

     $map_file_name = $upload_data['map_file']['file_name']; 
     $map_full_file_path = $path_to_uploads.'/'.$map_file_name; 

     $logo_file_name = $upload_data['logo_file']['file_name']; 
     $logo_full_file_path = $path_to_uploads.'/'.$logo_file_name; 
    } 

// run validation 
     if ($this->validation->run() == FALSE){ 
      $data['message'] = ''; 
     }else{ 
      // save data 
      $station = array('name' => $this->input->post('name'), 
          'rajar' => $rajar_full_file_path, 
          'playlist' => $playlist_full_file_path, 
          'map' => $map_full_file_path, 
          'logo' => $logo_full_file_path ); 
      $id = $this->stationModel->save($station); 

      // set form input name="id" 
      $this->validation->id = $id; 

      // set user message 
      $data['message'] = '<div class="success">add new station success</div>'; 
     } 

      $this->template->set('title', 'Admin - Add New Station!'); 
     $this->template->load('admin/template', 'admin/stationEdit', $data); 


    } 

表單提交後,我得到了以下錯誤......

消息:未定義的變量:rajar_full_file_path

消息:未定義的變量:playlist_full_file_path

消息:未定義變量:map_full_file_path

消息:未定義的變量:logo_full_file_path

我是愚蠢的,我無法弄清楚它?

感謝 丹

回答

1

看來,你的代碼是在進入第一個if語句,然後通過表單驗證(因此從不定義你的變量),然後嘗試在第二個else代碼塊中使用這些變量。

1

我好像文件上傳失敗,因此這些變量沒有被設置。顯示上傳錯誤的方法可能不起作用。會轉而錯誤發送回視圖:

代替:

$error = $this->upload->display_errors(); 
    echo "<script>alert($error);</script>"; 

嘗試:

$error = $this->upload->display_errors(); 
    $data['message'] = $error;