How to add multiple images of a post in database by cakephp 2x

I’m new to cakePHP, and I’m using cakePHP 2x. I’m doing a project where a user can upload multiple images using post. I have come so far but can’t upload the files.

This is the images table of my database:

post_id is the foreign key. This is the post model:


class Post extends AppModel {
    public $belongsTo = array('Catagory','User');
    public $hasMany = array(
        'Image' => array(
            'className' => 'Image',
            'foreignKey' => 'post_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''

Now the Post controller:

public function addPost() {

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

    // pr ($this->request->data);
    $rootfolder = WWW_ROOT . 'img/uploads/' ;

            //Check if image has been uploaded
            foreach ($this->request->data['Post']['Image'] as $i => $image) 
                $file = $this->request->data['Post']['Image'][$i];
                $tmp = $file['tmp_name'];

                // debug( $file);
                // debug( $tmp );
                // die();

                $ext = substr(strtolower(strrchr($file['name'], '.')), 1); 
                //get the extension

                $arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions

                    //only process if the extension is valid
                if(in_array($ext, $arr_ext))

                    $name = substr(md5(time()), 0,10).$i.'.'.$ext;
                    $path = $rootfolder.$name;
                    // die();
                    $file = array('name'=>$file['name'],'path' => $path );

                    $images[] = $file;

                    // $this->data['Post']['image']['name'] = $file['name']

                    // $this->Post->data['Image']['image'];

                    // $this->data->Post['Image']['image'] = $file['name'];

            // die();                   

            //now do the save (optional)
        }    //if($this->Image->save($this->data)) {...} else {...}

         // the save() method will check for validation errors and abort the save if any occur

        // pr ($this->request->data);
//      die();
    $this->request->data['Post']['user_id'] = AuthComponent::user('id');

    if ($this->Post->saveAll($this->request->data)) {
        $this->Flash->success(__('Your post has been saved.'));
            // redirect function redirects to another URL
        return $this->redirect(array('action' => 'index')); 
    $this->Flash->error(__('Unable to add your post.'));
$catagories = $this->Post->Catagory->find('list');
$image = $this->Post->Image->find('list');


Can anyone help me to complete these? Thank you.

Wouldn’t it be a better idea to just use regular html in the content body to load an image from the img/uploads folder?
Adding a specific table for all images per posts seems like it has a load of additional overhead…

how can you upload multiple images in the same table where post is ?

By just adding it into the HTML body.
add it into the HTML body like so:

This is a test message
<img src="" />

some more text here

but have the src attribute point to the image on the server (eg.

@FinlayDaG33k, the question is about uploading images through a form, not displaying them on the page.

1 Like

oh lol, my bad, was pretty hard to understand with the information I got :stuck_out_tongue:

but anyways, you can use a multipart form to send multiple images to the server.
Take a look here:

not so much helpful…images not uploading in the database but the post is uploaded to Post table.

What do you want to do?
Your code suggests that you want to put the actual image file in the uploads folder and store the reference to it (the path) in the database, but you say you want to upload them to the database?

This is highly confusing, which one is it?

Also, the code you’ve uploaded is very difficult to read due to poor indentation and commented out code.