CakePHP multiple file upload fields uploading only one image
I am trying to upload some files using a multiple upload file box. The POST information is being sent correctly and looks like this:
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
)
)
And I go through and insert each of them into the database and then load them using the ID from the database, like this:
// 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);
}
}
But only one image gets into the database and is loaded every time, can't decide why the look and feel isn't working correctly.
Any ideas? Thank.
+3
source to share