2017-09-15 93 views
0

嗨,我是CakePHP 3.0的初学者,我有一个问题,它不会保存在数据库中。在CakePHP 3.0中多次上传

控制器:

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!')); 
      } 
     } 
    } 
} 

有人可以帮我固定我的问题,我也跟着在CakePHP中的文档,但似乎不工作。

浏览:

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

这是我的数据库结构:

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; 

回答

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

它给了我一个错误未定义的类型,大小和名称 –

+0

能否请您张贴PR($ _ FILES) ;死;打印。 –

+0

我改变我的代码我用于循环,但保存不起作用。那么我的保存功能是这样的 $ table = TableRegistry :: get('Files'); $ entities = $ table-> newEntities($ file); $ table-> saveMany($ entities);但它不起作用 –