2016-12-11 10 views
1

ログインした使用IDに基づいて写真の名前と写真のディレクトリをユーザーテーブルに保存できません。既存のユーザーの写真をアップロードしようとしています彼の使用id.The写真のフィールドは、既存のユーザーのために更新されていません。このプラグインjosegonzalezを使って写真をアップロードしようとしています。私を助けてください。Cakephp 3.3 - プラグインを使用して画像を保存できません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); 
     } 

答えて

0

フォームタグに「enctype」を使用してみましたか?

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

hereです。

+0

私の悪い、<?php echo $ this-> Form-> create($ user、['type' => 'file']); ?>はenctypeを生成します – Aarrbee

+0

はい私はそれを試しました。ありがとう!それは動作しませんでした – Mythri

+0

あなたのコントローラにthis-> request-> dataのデバッグを投稿できますか?それは私がもっと理解するのを助けるでしょう。 – Aarrbee

0

Profferプラグインを使用できます。使いやすく、サムネイルの生成にも使用できます。私は3.1以降、それを使用しています。それも、これは、それが働いたかである3.3

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

0

に取り組んでいます。私はプラグインを一切使っていませんでした。私は一度に一つの画像をアップロードしました。

UsersController/add関数。

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); 
} 
関連する問題