Ketan Patel

Multiple Images Upload in CakePHP


Multiple Images Upload in CakePHP

In this post we will see how to upload 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/' . time().$file['name']);
       $this->request->data['Post']['image'.$i] = time().$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/' . time().$file['name']);
       $this->request->data['Post']['image'.$i] = time().$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;
  }
 }



Note : As of PHP 5.1, when called with no arguments, mktime() throws an E_STRICT notice: use the time() function instead.


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

ketan patel

About Ketan Patel -

I have developed a wide range of websites using CorePHP, Opencart, CakePHP and CodeIgniter including sites for startup companies and small businesses. Apart from my blogging life, I like to read Novels, Listening music and Net surfing.

Subscribe to this Blog via Email :