1

我无法根据登录的使用 ID 在用户表中保存照片名称和照片目录。我正在尝试根据现有用户使用的 ID 上传用户照片。照片字段没有更新现有用户。我正在尝试使用此插件 josgonzalez 上传照片。请帮我。

<?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);
         }
4

3 回答 3

0

您是否尝试过在表单标签中使用“enctype”?

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

它在这里解释。

于 2016-12-12T06:16:26.333 回答
0

这就是它的工作原理。我没有使用任何插件。我一次上传一张图片。

用户控制器/添加功能。

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);
}
于 2017-01-16T10:33:25.670 回答
0

你可以使用Proffer插件。它易于使用,也可用于生成缩略图。我从 3.1 开始使用它并且工作正常。它甚至适用于 3.3

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

于 2016-12-31T16:10:58.617 回答