2013-07-25 10 views
0

我做了一個上傳腳本來上傳多張圖片。這很好,但我想將文件名添加到數據庫。如何將所有上傳圖像的文件名添加到Codeigniter中的數據庫中?

我的問題是,只有上傳的最後一張圖片才被添加到數據庫中。 這是因爲我不知道如何將數據數組從控制器傳遞到模型。

我的代碼上傳看起來像這樣(控制器):

public function aanbiedingenadd() //functie om ze toe te voegen aan de database 
{  
    $files = $_FILES; 
    echo '<pre>'; 
    print_r($files); 
    $cpt = count($_FILES['userfile']['name']); 
    for($i=0; $i<$cpt; $i++) 
    { 

     $_FILES['userfile']['name']= $files['userfile']['name'][$i]; 
     $_FILES['userfile']['type']= $files['userfile']['type'][$i]; 
     $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i]; 
     $_FILES['userfile']['error']= $files['userfile']['error'][$i]; 
     $_FILES['userfile']['size']= $files['userfile']['size'][$i];  

     $this->upload->initialize($this->set_upload_options()); 
     $this->upload->do_upload(); 
     $image_data = $this->upload->data(); 
    } 
    print_r($image_data); 
    print_r($this->upload->display_errors()); 
    $this->aanbieding_model->addaanbieding($image_data); 
    redirect("members/aanbiedingen"); 
} 

print_r($files)顯示我所有的大小,寬度,高度等,上傳的文件

print_r($image_data);只顯示最近一次上傳的圖像。

我的模型看起來是這樣的:

 $insert_data = array(
      'fotonaam' => $image_data['file_name'] 
     ); 
     print_r($insert_data); 
     die; 
     $input = $this->input->post('userfile'); 
     if(isset($input)){ 
      $this->db->insert('fotoaanbiedingen', $insert_data); 
     }else{ 
      return FALSE; 
     } 

print_r($insert_data);顯示我

Array 
(
    [fotonaam] => 'nameoflastuploadedimage' 
) 

我如何在Array添加這個,添加逐一?

回答

1

for loop這裏面的臺詞:

$this->aanbieding_model->addaanbieding($image_data); 
+0

現在只顯示了'的print_r($ IMAGE_DATA)的第一個上傳的圖像;' –

+0

好的,這解決了這個問題,但現在我必須編輯我的模型,以便爲相同的aanbieding添加圖像。它爲每個圖像添加一個新行。我希望這些圖像只能用於1 aanbieding。 –

+0

不應該「表內的東西」比這個數組裏面的東西更重要嗎? 但只是爲了澄清:每次調用for循環時,$ image_data都會被uploaded_data()覆蓋。所以很自然你只能看到這個數組中的最後一張圖片。也許你應該清楚基本數組的東西:http://www.php.net/manual/en/language.types.array.php – mgrueter

0

移動$this->aanbieding_model->addaanbieding($image_data);裏面的for循環,這基本上它。

如果您獲得大量圖片上傳,您可能應該使用陣列來收集所有圖片數據,然後在您的模型中使用insert_batch()

例如在您的控制器中:

$images = array(); 
$files = $_FILES; 
$cpt = count($_FILES['userfile']['name']); 
for($i=0; $i<$cpt; $i++) 
{ 
    // your upload routine here 
    $images[] = $this->upload->data(); 
} 

$this->aanbieding_model->addaanbieding($images); 

而在您的模型中,您將使用$this->db->insert_batch('fotoaanbiedingen', $insert_data);而不是僅插入。

希望有幫助!

+0

你的例子顯示$ this-> aanbieding_model-> addaanbieding($ images);在for循環之外。 –

+0

是的,這個例子是說明模型中'insert_batch'的用法。不同之處在於,在for循環中移動模型調用(使用insert)將會爲每個上傳文件提供1個查詢,而'insert_batch'只會爲所有上傳的文件創建1個查詢。 – mgrueter

+0

好吧,它現在可以工作,但是當我上傳3張圖片時,創建了3行,我想要用3張圖片創建1行。 '我忘了提到我使用了連接表,所以1 aanbieding可以有更多的圖像(應該是) –

0

請這樣的finction: -

public function fileUpload(&$uploadFileData,$field) 
{ 
    $config1['allowed_types'] = 'gif|jpg|jpeg|png'; 
    $config1['upload_path']  = ABSOLUTEPATH.'category/original/'; 
    $config1['optional']  = true; 
    $config1['max_size']  = '0'; 
    $config1['max_width']  = '00'; 
    $this->load->library('upload', $config1); 
    if (!$this->upload->do_upload($field,true)) 
    { 
     $error = array('error' => $this->upload->display_errors()); 
     $r = $error; 
    } 
    else 
    { 
     $data = array('upload_data' => $this->upload->data()); 
     $r = "sucess"; 
    } 
    $uploadFileData[$field] = $this->upload->file_name; 
    return $r; 
} 

,並調用它,如: -

$field = 'userfile'; 
    $uploadFileData = array(); 
    $uploadFileData[$field.'_err'] = ''; 
    $isUploaded = $this->fileUpload($uploadFileData,$field); 
    if($isUploaded != "sucess") 
    { 
    echo "error"; 
    } 
    else 
    { 
    $imagename = $uploadFileData[$field]; 
    } 
+0

爲什麼改變整個代碼? -1 –

+0

不改變整個代碼,你只需要改變$ this-> upload-> initialize($ this-> set_upload_options()); $ this-> upload-> do_upload(); $ image_data = $ this-> upload-> data(); – Prasannjit

+0

我不會更改我的整個代碼。無論如何它不會起作用。 –

相關問題