3
我想使用多個上傳文件字段上傳一些文件。該發佈信息被正確發送,看起來是這樣的:CakePHP多個文件上傳字段只能上傳一個圖像
Array
(
[Uploads] => Array
(
[photos] => Array
(
[0] => Array
(
[name] => image - Copy - Copy.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpALAMwT
[error] => 0
[size] => 60892
)
[1] => Array
(
[name] => image - Copy.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpoIGtta
[error] => 0
[size] => 60892
)
[2] => Array
(
[name] => image.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpERTogu
[error] => 0
[size] => 60892
)
)
而我遍歷並插入每一個到數據庫中,然後將它們上傳使用的ID從數據庫中,像這樣:
// Upload Photos
if (!empty($this->request->data['Uploads']['photos'][0]['tmp_name'])){
foreach($this->request->data['Uploads']['photos']as $photo){
$property_id = $this->request->data['Property']['ID'];
$file_name = $photo['name'];
$file_size = $photo['size'];
$file_ext = pathinfo($photo['name'], PATHINFO_EXTENSION);
// Save to DB
$this->Property->PropertyImage->save(array(
'PropertyImage' => array("Live"=>1, 'Number'=>99, "Type"=>'L', "FileType"=>$file_ext, "PropertyID"=>$property_id, 'Source'=>$file_name, 'Size=>'.$file_size)
));
// Upload
$id = $this->Property->PropertyImage->getLastInsertID();
$path = intval($id/1000) . '/' . $id . '.' . $file_ext;
move_uploaded_file($photo['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/imgp/F/'.$path);
}
}
但是每次只有一個圖像被放入數據庫並上傳,無法弄清楚爲什麼外觀不能正常工作。
任何想法?謝謝。
getLastInsertID()停止工作,當我這樣做? – 472084 2012-03-09 16:56:41
爲什麼要停止?如果你期望很多,我還會推薦一個更好的方法來存儲圖像。我最近寫了一篇關於某篇文章:http://cakedc.com/florian_kraemer/2012/02/03/image-saving-and-processing-for-high-traffic-sites – burzum 2012-03-09 16:58:39
對不起,看錯做什麼。謝謝,這固定了它! – 472084 2012-03-09 17:07:42