2017-09-15 3 views
0

Hallo ich bin ein Anfänger in CakePHP 3.0 mein Ich habe ein Problem, wird es nicht in der Datenbank speichern.Mehrere hochladen in CakePHP 3.0

Controller:

public function add() { 
    $myTable = TableRegistry::get('Files'); 
     $data = $this->request->getData(); 
     $file = $myTable->newEntities($data); 
    if ($this->request->is('post')) { 
     foreach ($file as $key => $val) { 
      $val['user_id'] = $this->Auth->user('id'); 
      $val['filename'] = $_FILES['photo']['name']; 
      $val['file_location'] = '../uploads/files/'; 
      $val['file_type'] = $_FILES['photo']['type']; 
      $val['file_size'] = $_FILES['photo']['size']; 
      $val['file_status'] = 'Active'; 
      $val['created'] = date("Y-m-d H:i:s"); 
      $val['modified'] = date("Y-m-d H:i:s"); 

      if($myTable->save($val)) { 
       $this->Flash->success(__('File has been uploaded and inserted successfully.')); 
      } else { 
       $this->Flash->error(__('File not upload!')); 
      } 
     } 
    } 
} 

kann mir jemand helfen, mein Problem zu festen, folgte ich die Dokumentation in CakePHP, aber es scheint nicht funktioniert.

Aufrufe:

<?= $this->Form->create(null, ['type' => 'file', 'url' => ['controller' => 'Files', 'action' => 'add']]) ?> 
echo $this->Form->input('photo[]', ['type' => 'file','multiple' => 'true','label' => 'Upload Multiple Photos']); 

Dies ist meine Datenbankstruktur:

CREATE TABLE `files` (
    `id` int(8) NOT NULL, 
    `user_id` varchar(100) NOT NULL, 
    `filename` varchar(100) NOT NULL, 
    `file_location` varchar(1000) NOT NULL, 
    `file_type` varchar(100) NOT NULL, 
    `file_size` varchar(100) NOT NULL, 
    `file_status` varchar(100) NOT NULL, 
    `created` datetime NOT NULL, 
    `modified` datetime NOT NULL, 
    `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

Antwort

0
public function add() { 
    $myTable = TableRegistry::get('Files'); 
    if ($this->request->is('post')) { 
     foreach ($_FILES['photo'] as $key => $file) { 
      $val['user_id'] = $this->Auth->user('id'); 
      $val['filename'] = $file['name']; 
      $val['file_location'] = '../uploads/files/'; 
      $val['file_type'] = $file['type']; 
      $val['file_size'] = $file['size']; 
      $val['file_status'] = 'Active'; 
      $val['created'] = date("Y-m-d H:i:s"); 
      $val['modified'] = date("Y-m-d H:i:s"); 


      $val = $myTable->newEntity($val); 
      //Make some count here to trace the no of files uploaded and take this if else out of this foreach, otherwise it will display many flash message. 
      if($myTable->save($val)) { 
       $this->Flash->success(__('File has been uploaded and inserted successfully.')); 
      } else { 
       $this->Flash->error(__('File not upload!')); 
      } 
     } 
    } 
} 
+0

es mir eine Fehler undefinierte Art, Größe und Namen –

+0

Könnten Sie bitte posten, dass pr ($ _ FILES) gibt ;sterben; Drucke. –

+0

ich ändere meinen Code ich benutze für Schleife, aber speichern funktioniert nicht. dann ist meine Speicherfunktion das $ table = TableRegistry :: get ('Files'); $ entities = $ table-> newEntities ($ file); $ table-> saveMany ($ entities); aber es funktioniert nicht –