2016-12-11 1 views
1

로그인 한 사용 ID를 기반으로 사용자 테이블에 사진 이름과 사진 디렉토리를 저장할 수 없습니다. 기존 사용자 기반의 사용자 사진을 업로드하려고합니다. 기존 사용자의 사진 필드가 업데이트되지 않습니다. 이 플러그인 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

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

에서 일하고있어. 나는 어떤 플러그인도 사용하지 않았다. 한 번에 하나의 이미지를 업로드했습니다.

사용자 컨트롤러/추가 기능.

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); 
} 
관련 문제