我做了一個上傳腳本來上傳多張圖片。這很好,但我想將文件名添加到數據庫。如何將所有上傳圖像的文件名添加到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添加這個,添加逐一?
現在只顯示了'的print_r($ IMAGE_DATA)的第一個上傳的圖像;' –
好的,這解決了這個問題,但現在我必須編輯我的模型,以便爲相同的aanbieding添加圖像。它爲每個圖像添加一個新行。我希望這些圖像只能用於1 aanbieding。 –
不應該「表內的東西」比這個數組裏面的東西更重要嗎? 但只是爲了澄清:每次調用for循環時,$ image_data都會被uploaded_data()覆蓋。所以很自然你只能看到這個數組中的最後一張圖片。也許你應該清楚基本數組的東西:http://www.php.net/manual/en/language.types.array.php – mgrueter