Cakephp 3.3 - 无法使用插件josegonzalez保存图像

时间:2016-12-11 16:39:39

标签: cakephp cakephp-3.3

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

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

3 个答案:

答案 0 :(得分:0)

您是否尝试过使用&#39; enctype&#39;在你的表格标签?

{
         headerName: 'Salary', field: 'sal'
        cellRenderer: this.CurrencyCellRenderer
       }

private CurrencyCellRenderer(params:any) {

    var usdFormate = new Intl.NumberFormat('en-US', {
        style: 'currency',
        currency: 'USD',
        minimumFractionDigits: 4
    });
    return usdFormate.format(params.value);
}

解释http://nshipster.com/network-link-conditioner/

答案 1 :(得分:0)

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

上工作

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

答案 2 :(得分:0)

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

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