i have two tables
product and productimage table..
i want to insert multiple images in product image table.
below is my controller code...
the code is working but it is only uploading single image...in my table..
But i want to upload multiple images..i amusing cakephp 2.x
plz help me to resolve this...
public function addProduct() {
if (is_uploaded_file($this->request->data['Product']['file']['tmp_name'])) {
$pathToUpload = "img/Frontend/Products/" . $this->Auth->User('Profile.user_id') . '/';
$pathToUploadThumbnail = "img/Frontend/Products/" . $this->Auth->User('Profile.user_id') . "/Thumbnails/";
if (!(is_dir($pathToUpload))) {
mkdir($pathToUpload, 0777);
mkdir($pathToUploadThumbnail, 0777);
}
$fileName = $this->request->data['Product']['file']['name'];
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
$this->request->data['Product']['file_name'] = $this->request->data['Product']['title'] . time() . '.' . $ext;
$fileName = str_replace('/', 'A', $this->request->data['Product']['file_name']);
move_uploaded_file($this->request->data['Product']['file']['tmp_name'], $pathToUpload . $fileName);
}
if ($this->Product->save($productData)) {
$this->loadModel('ProductImage');
if (!empty($this->request->data['Product']['file_name'])) {
$productImage['ProductImage']['product_id'] = $this->Product->getLastInsertID();
$productImage['ProductImage']['image'] = $fileName;
$this->ProductImage->save($productImage);
}
$this->Session->setFlash('Product has been added.');
$this->redirect('productListing');
}
}
}
?>
**photo upload button**
<?php
echo $this->Form->input('Product.file', array('type' => 'file','label'=>false,'div'=>false,'class'=>'input_bar'));
?>
I am using below script to generate browse button
<script type="text/javascript">
$(document).ready(function() {
$('#more').on('click', function(){
var newfield = '<div class="keyword"><input type="file" class="input_bar" name="data[Product][file][]"><button class="add_more_img btn btn-lg btn-primary update remove">Remove</button></div>';
$('#main').append(newfield);
});
$(document).on('click','.remove', function(){
$(this).parent('div').remove();
});
});
</script>
Ello, mate. You can arrange your product images into an array, such like:
$data_to_save = array(
array('data' => 'value', 'data' => 'value'),
array('data' => 'value', 'data' => 'value')
);
And then you call the method saveMany() instead save()
$this->ProductImage->saveMany($data_to_save);