Multiple Images Upload in CakePHP



In this post we will see how to uload multiple images in cakephp.I am going to show the additional code for multiple image handling for both the action Add and Edit.What all you need is to simply copy paste in your application according to needs.

My table : posts

CREATE TABLE `posts` (
`id` int(10) NOT NULL AUTO_INCREMENT,

`title` varchar(200) NOT NULL,

`body` text NOT NULL,

`image1` varchar(200), 

`image2` varchar(200), 

`image3` varchar(200),
PRIMARY KEY (`id`)
)

As you can see what are the fields of my 'posts' table.I have three fields for the images.Example: image1,image2,image3 etc.You may have different fields. Add the code given below for multiple image handling.

My uploaded images are stored in  app/webroot/img/uploads/posts  folder.I have one default image called 'no-icon.jpg' in the same folder which will be shown if the correct image is not available or in case of NULL value.

Controller : PostsController.php


Add Function

public function add() {

$this->Post->create();

if ($this->request->is('post')) {



// Image Handling code START //////

for($i=1;$i<4;$i++)

{

if(empty($this->data['Post']['image'.$i]['name'])){
unset($this->request->data['Post']['image'.$i]);
}
if(!empty($this->data['Post']['image'.$i]['name']))
{
$file=$this->data['Post']['image'.$i];
$ary_ext=array('jpg','jpeg','gif','png'); //array of allowed extensions
$ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
if(in_array($ext, $ary_ext))
{
move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img/uploads/posts/' . mktime().$file['name']);
$this->request->data['Post']['image'.$i] = mktime().$file['name'];
}
}
}
// Image Handling code END //////

  if ($this->Post->save($this->request->data)) 
  {
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
}
else 
{
$this->Session->setFlash('Unable to add your post.');
}
}
}


Edit Function

public function edit($id=null){

if(!$id)

{

throw new NotFoundException(__('Invalid Post'));

}



$post=$this->Post->findById($id);
if(!$post)
{
throw new NotFoundException(__('Invalid Post'));
}
if(!empty($this->data))
{
$this->Post->id=$id;
                                
// Image Handling code START //////
for($i=1;$i<4;$i++)
{
if(empty($this->data['Post']['image'.$i]['name'])){
unset($this->request->data['Post']['image'.$i]);
}
if(!empty($this->data['Post']['image'.$i]['name']))
{
if(file_exists("img/uploads/posts/".$this->data['Post']['hiddenimage'.$i])){
  unlink("img/uploads/posts/".$this->data['Post']['hiddenimage'.$i]);
    }
$file=$this->data['Post']['image'.$i];
$ary_ext=array('jpg','jpeg','gif','png'); //array of allowed extensions
$ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
if(in_array($ext, $ary_ext))
{
move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img/uploads/posts/' . mktime().$file['name']);
$this->request->data['Post']['image'.$i] = mktime().$file['name'];
}
}
}
        // Image Handling code END //////
if($this->Post->save($this->request->data))
{
$this->Session->setFlash('Your Post has been Updated');
$this->redirect(array('action'=>'index'));
}
else
{
$this->Session->setFlash('Unable to update your post.');
}
}
if(!$this->request->data){
$this->request->data=$post;
}
}


I can't post all the code of view files so for that please Download the complete source code from the below link.

Download Full Source

Comments

Popular posts from this blog

How to show only month and year fields in android Date-picker?

How to construct a B+ tree with example

Conflict Serializability in database