2016-11-06 5 views
0

cakephp 3.xアプリケーションでイメージをアップロードしようとしています。画像をアップロードしていないときは、他のデータはうまく保存されていましたが、ファイルアップロードの統合以来、画像のアップロード機能は正常に機能していますが、他のデータは保存されません。以下はサンプルコードです。見つけられない小さな問題しかないかもしれないと思うので、ご覧ください。ここでは、コントローラでの追加アクションのコードは次のとおりです。ここでCakePHP3でイメージのアップロードとデータの競合が発生する

$team_member = $this->TeamMembers->newEntity(); 
    if ($this->request->is('post')) { 
     if (!empty($this->request->data['photo']['name'])) { 
      $photo = $this->request->data['photo']; 

      $ext = substr(strtolower(strrchr($photo['name'], '.')), 1); //get the extension 
      $arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions 
      $setNewFileName = time() . "_" . rand(000000, 999999); 

      //only process if the extension is valid 
      if (in_array($ext, $arr_ext)) { 
       //do the actual uploading of the file. First arg is the tmp name, second arg is 
       //where we are putting it 
       move_uploaded_file($photo['tmp_name'], WWW_ROOT . '/uploads/' . $setNewFileName . '.' . $ext); 
       //prepare the filename for database entry 
       $imageFileName = $setNewFileName . '.' . $ext; 
      } 
     } 

     $team_member = $this->TeamMembers->patchEntity($team_member, $this->request->data); 
     if (!empty($this->request->data['photo']['name'])) { 
      $team_member->photo = $imageFileName; 
     } 
     if ($this->TeamMembers->save($team_member)) { 
      $this->Flash->success(__('Saved Successfully.')); 
      return $this->redirect([ 
        'action' => 'index' 
      ]); 
     } 
     $this->Flash->error(__('Failed to save. Try Again.')); 
    } 
    $this->set('team_member', $team_member); 

はadd.ctpファイルです:

echo $this->Form->create($team_member, ['enctype' => 'multipart/form-data']); 
echo $this->Form->input('name'); 
echo $this->Form->input('photo', array('type' => 'file')); 
echo $this->Form->input('excerpt'); 
echo $this->Form->input('content'); 
echo $this->Form->button('Save', ['class' => 'btn']); 
echo $this->Form->end(); 

私は、ファイルの任意のコードを書いていなかったとき、以前のそれは正常に動作していることに注意してくださいアップロードする。そして、CakePHPのバージョンが3.xの

TIA

答えて

-2

私は答えを得ています。私は論理的にこれが間違っているので、なぜそれが動作しているのかわかりませんが、今は正常に動作しています。 は、私は、とにかく

echo $this->Form->create($team_member); 

にadd.ctpファイル

echo $this->Form->create($team_member, ['enctype' => 'multipart/form-data']); 

にこのコード行を置き換え、それは今、正常に動作しています。しかし、ファイルアップロードを行うすべてのフォームがmultipart/form-dataとしてenctypeを持つ必要があるため、理由が分かっても問題は解決できます。

+0

通常、 '$ this-> Form-> create($ team_member、['type' => 'file']))'が必要です。私は、生成されたフォームに適切なエンコーディングタイプが設定されていないとうまくいくのではないかと疑います。 ** http://book.cakephp.org/3.0/en/views/helpers/form.html#changing-the-http-method-for-a-form** – ndm

+0

私はenctypeはここで定義する必要がありますが、まだenctypeを追加したり、['type' => 'file']を定義しても機能しません。私はこのヘルパーリンクを読んで何度も本を紹介してきましたが、まだそれはうまく機能していません。しかし、とにかく、この間違ったコードで、今働いています。それがなぜ起こるのか分かっているなら、私に知らせてください。 –

関連する問題