2017-09-14 9 views
0

こんにちは誰かがこれを知っています。私はCakephpの初心者です。複数の画像をアップロードしようとしましたが、保存はできません。配列を保存するcakephp 3 savemany

コントローラー:

public function add() { 
if ($this->request->is('post')) { 
    //$data = $this->request->getData(); 
    if(!empty($_FILES['photo']['name'])){ 
     $count = count($_FILES['photo']['name']); 
     for ($i=0; $i < $count; $i++) { 
      $filename = $_FILES['photo']['name'][$i]; 
      $type = $_FILES['photo']['type'][$i]; 
      $tmp = $_FILES['photo']['tmp_name'][$i]; 
      $error = $_FILES['photo']['error'][$i]; 
      $size = $_FILES['photo']['size'][$i]; 
      $uploadPath = '../uploads/files/'; 

      $file[$i]['user_id'] = $this->Auth->user('id'); 
      $file[$i]['filename'] = $filename; 
      $file[$i]['file_location'] = $uploadPath; 
      $file[$i]['file_type'] = $type; 
      $file[$i]['file_size'] = $size; 
      $file[$i]['file_status'] = 'Active'; 
      $file[$i]['created'] = date("Y-m-d H:i:s"); 
      $file[$i]['modified'] = date("Y-m-d H:i:s"); 
     } 
     $table = TableRegistry::get('files'); 
     $entities = $table->newEntities($file); 
     if($table->saveMany($entities)) { 
      $this->Flash->success(__('File has been uploaded and inserted successfully.')); 
      return $this->redirect(['action' => 'index']); 
     } else { 
      $this->Flash->error(__('Unable to upload file, please try again.')); 
     } 

    } else { 
     $this->Flash->error(__('Please choose a file to upload.')); 
    } 
} 

} しかし、私はすべての良いが、それは文句を言わない仕事保存でデバッグしようとしました!あなたが一度saveMany()を使用して一つのレコードを保存しようとしている

echo $this->Form->input('photo[]', ['type' => 'file','multiple' => 'true','label' => 'Upload Multiple Photos']); 

答えて

0

:私のコードに問題がない、誰かが私の追加機能

ビューを修正する方法私を助けることができます。

public function add() 
{ 
    if ($this->request->is('post')) { 
     $table = TableRegistry::get('files'); 
     $uploadPath = '../uploads/files/'; 

     if(!empty($_FILES['photo'])){ 
      foreach ($_FILES['photo'] as $EachPhoto) { 
       $data[] = [ 
        'user_id' => $this->Auth->user('id'), 
        'filename' => $EachPhoto['name'], 
        'file_location' => $uploadPath, 
        'file_type' => $EachPhoto['type'], 
        'file_size' => $EachPhoto['size'], 
        'file_status' => 'Active', 
        'created' => date("Y-m-d H:i:s") 
       ]; 
      } 

      $entities = $table->newEntities($data); 
      if($this->Files->saveMany($entitie)) { 
        $this->Flash->success(__('File has been uploaded and inserted successfully.')); 
        return $this->redirect(['action' => 'index']); 
      } else { 
        $this->Flash->error(__('Unable to upload file, please try again.')); 
      } 

     } else { 
      $this->Flash->error(__('Please choose a file to upload.')); 
     } 
    } 
} 
+0

私のコードを再構築しますが、同じ結果を返します。データベースに保存するとエラーが発生します。 –

+0

ここに投稿する$ _FILESと$ this-> request-> data();印刷中です。 –

+0

私のコードを一番上に編集しています。$ _FILESは大域変数で、ファイルを取得するには –

関連する問題