user2301
user2301

Reputation: 1967

Cakephp 3.3 - Not able to save images using plugin josegonzalez

I am not able to save the photo name and photo dir in the users table based on logged in used id.I am trying to upload the user photo for the existing user based on his used id.The photo fields are not getting updated for the existing user. I am trying to upload the photo using this plugin josegonzalez. Please help me.

<?php echo $this->Form->create($user, ['type' => 'file']); ?>
        <?php echo $this->Form->input('photo',['type' => 'file', 'class' => 'form-control']); ?>
        <?php echo $this->Form->input('photo_dir', ['type' => 'hidden']); ?>
        <?php echo $this->Form->button(__('Submit'), ['type'=>'submit','class' => 'btn btn-success']); ?>
        <?php echo $this->Form->end(); ?>

 UsersTable
 $this->addBehavior('Josegonzalez/Upload.Upload', [
        'photo' => [
            'fields' => [
                // if these fields or their defaults exist
                // the values will be set.
                'dir' => 'photo_dir', // defaults to `dir`

            ],
        ],
    ]);

    UsersController/add
    public function add($id=null)
  {
if ($this->request->is('post')) {
if (!$id) {
            $id = $this->Auth->user('id');
        }
        $user = $this->Users->get($id);
        $fileName = $this->request->data['photo']['name'];
          $user->photo = $fileName;       
     //$user = $this->Users->patchEntity($user, $this->request->data);
     if ($this->Users->save($user)) {
           $this->Flash->success(__('Your photo has been saved.'));
           return $this->redirect(['action' => 'index']);
     }
         $this->Flash->error(__('Unable to add your photo.'));
        }
        $this->set('user', $user);
         }

Upvotes: 1

Views: 216

Answers (3)

user2301
user2301

Reputation: 1967

This is how it worked. I did not use any plugin. I uploaded single image at a time.

UsersController/add function.

public function add()
{
    //check for logged in user authentication
    $id = $this->Auth->user('id');
    $user = '';
    //check if the request is post
    if ($this->request->is('post')) {
        $user = $this->Users->get($id);
            //check if upload file is not empty
             if(!empty($this->request->data['photo']['name'])){
                $fileName = $this->request->data['photo']['name'];
                $extention = pathinfo($fileName,PATHINFO_EXTENSION);
                $arr_ext = array('jpg', 'jpeg', 'gif','png');
                //check for uploded file extension
                if(in_array($extention, $arr_ext)){
                $newfileName=$id.'.'.$extention;
                $destDir = WWW_ROOT .'img'. DS .'users'. DS . $newfileName;
                //move uploded file to destination
                if(move_uploaded_file($this->request->data['photo']['tmp_name'],$destDir)){
                $user->photo = $newfileName;
                //save the uploded image in user table
                if ($this->Users->save($user)) {
                $this->Flash->success(__('Your profile photo has been uploaded successfully.'));
                return $this->redirect([
                        'controller' => 'Users',
                        'action' => 'view', $id
                         ]);
                } else{
                    $this->Flash->error(__('Unable to upload image, please try again.'));
                }
                }else{
                $this->Flash->error(__('Unable to upload image, please try again.'));
                }
                }else{
                $this->Flash->error(__('Please choose a image to upload.'));
            }
        }
    }
    $this->set(compact('user'));
    $this->set('_serialize', ['user']);
    $this->set('id', $id);
}

Upvotes: 0

Anuj TBE
Anuj TBE

Reputation: 9790

You can use Proffer plugin. It is easy to use and can be used to generate thumbnails too. I'm using it since 3.1 and working fine. It's even working on 3.3

https://github.com/davidyell/CakePHP3-Proffer

Upvotes: 0

Disorder
Disorder

Reputation: 430

Have you tried using 'enctype' in your form tag?

<form enctype="multipart/form-data">

It is explained here.

Upvotes: 0

Related Questions