CakePHP Upload Multiple Gambar ke Database dan Menampilkannya
Berikut penulis akan menjelaskan cara upload multiple gambar melalui komputer di Framework CakePHP dan menampilkannya kembali.
Struktur tabel database images:
CREATE TABLE `images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=903 DEFAULT CHARSET=latin1;
Pada file View, copy code berikut ini:
<?php
echo ($this->Form->create('Image', array('name' => 'NormalImport', 'url' => array('controller' => 'uploads', 'action' => 'upload_file')));
echo $this->Form->input('user_image.',array('type'=>'file','label' => false,'placeholder' => 'Upload images','id'=>"inputFile",'multiple','onchange'=>'readURL(this)'));?>
echo($this->Form->submit('Upload File', array('div'=>false, 'class'=>'btn btn-primary pull-left')));
echo($this->Form->end());
?>
Bentuk atau struktur gambar yang diupload akan terlihat sebagai berikut. Semakin banyak gambar yang akan diupload maka jumlah array akan bertambah.
Array
(
[Model] => Array
(
[files] => Array
(
[0] => Array
(
[name] => MY.jpg
[type] => image/jpeg
[tmp_name] => // temp path
[error] => 0
[size] => 1826587
)
[1] => Array
(
[name] => YOU.jpg
[type] => image/jpeg
[tmp_name] => // temp path
[error] => 0
[size] => 1127346
)
)
)
Maka untuk menyimpan gambar hanya tinggal melakukan fungsi perulangan sebanyak gambar yang dipilih. Pada file controller anda ketik code berikut:
public function admin_upload_file($quiz_id = null, $type = null, $user_id = null)
{
$_file = $this->request->data['Image']['user_image'];
$n = count($this->request->data['Image']['user_image']);
if(!empty($this->data['Image']['user_image']))
{
for($i= 0;$i < $n; $i++) {
$file=$this->request->data['Image']['user_image'];
$ary_ext=array('jpg','jpeg','png','PNG','JPG','JPEG');
$ext = substr(strtolower(strrchr($file[$i]['name'], '.')), 1); if(in_array($ext, $ary_ext)) {
move_uploaded_file($file[$i]['tmp_name'], WWW_ROOT . IMAGE_FILE_FULL_DIR . DS . time().$file[$i]['name']);
$name = time().$file[$i]['name'];
$name_arr[$i] = $name;
$imagePath = IMAGE_PATH_FOR_TIM_THUMB.'/'.IMAGE_FILE_FULL_DIR.'/';
$imageData['Image']['name'] = $imagePath.$name; $this->Image->create();
$this->Image->save($imageData);
}
}
if(in_array($ext, $ary_ext))
{
$this->Session->setFlash("File Imported successfully", 'admin_flash_good');
}else
{
$this->Session->setFlash(__('Please upload an image file.', true), 'admin_flash_bad');
}
}
else
{
$this->Session->setFlash(__('Please upload an image file.', true), 'admin_flash_bad');
}
}
Untuk Menampilkan gambar, gunakan fungsi img src:
<?php
echo "<img src='".$value['Image']['name']."'' width='200px' height='70px'></img>";
?>
Struktur tabel database images:
CREATE TABLE `images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=903 DEFAULT CHARSET=latin1;
Pada file View, copy code berikut ini:
<?php
echo ($this->Form->create('Image', array('name' => 'NormalImport', 'url' => array('controller' => 'uploads', 'action' => 'upload_file')));
echo $this->Form->input('user_image.',array('type'=>'file','label' => false,'placeholder' => 'Upload images','id'=>"inputFile",'multiple','onchange'=>'readURL(this)'));?>
echo($this->Form->submit('Upload File', array('div'=>false, 'class'=>'btn btn-primary pull-left')));
echo($this->Form->end());
?>
Bentuk atau struktur gambar yang diupload akan terlihat sebagai berikut. Semakin banyak gambar yang akan diupload maka jumlah array akan bertambah.
Array
(
[Model] => Array
(
[files] => Array
(
[0] => Array
(
[name] => MY.jpg
[type] => image/jpeg
[tmp_name] => // temp path
[error] => 0
[size] => 1826587
)
[1] => Array
(
[name] => YOU.jpg
[type] => image/jpeg
[tmp_name] => // temp path
[error] => 0
[size] => 1127346
)
)
)
Maka untuk menyimpan gambar hanya tinggal melakukan fungsi perulangan sebanyak gambar yang dipilih. Pada file controller anda ketik code berikut:
public function admin_upload_file($quiz_id = null, $type = null, $user_id = null)
{
$_file = $this->request->data['Image']['user_image'];
$n = count($this->request->data['Image']['user_image']);
if(!empty($this->data['Image']['user_image']))
{
for($i= 0;$i < $n; $i++) {
$file=$this->request->data['Image']['user_image'];
$ary_ext=array('jpg','jpeg','png','PNG','JPG','JPEG');
$ext = substr(strtolower(strrchr($file[$i]['name'], '.')), 1); if(in_array($ext, $ary_ext)) {
move_uploaded_file($file[$i]['tmp_name'], WWW_ROOT . IMAGE_FILE_FULL_DIR . DS . time().$file[$i]['name']);
$name = time().$file[$i]['name'];
$name_arr[$i] = $name;
$imagePath = IMAGE_PATH_FOR_TIM_THUMB.'/'.IMAGE_FILE_FULL_DIR.'/';
$imageData['Image']['name'] = $imagePath.$name; $this->Image->create();
$this->Image->save($imageData);
}
}
if(in_array($ext, $ary_ext))
{
$this->Session->setFlash("File Imported successfully", 'admin_flash_good');
}else
{
$this->Session->setFlash(__('Please upload an image file.', true), 'admin_flash_bad');
}
}
else
{
$this->Session->setFlash(__('Please upload an image file.', true), 'admin_flash_bad');
}
}
Untuk Menampilkan gambar, gunakan fungsi img src:
<?php
echo "<img src='".$value['Image']['name']."'' width='200px' height='70px'></img>";
?>
Comments
Post a Comment