2015-06-30 67 views
1

我想通過表單保存圖像路徑,但圖像路徑未保存。用cakephp3保存圖像

我試着this,但我不知道在哪裏把代碼

MultimediaController:

public function add() 
{ 
    $multimedia = $this->Multimedia->newEntity(); 
    if ($this->request->is('post')) { 
     $multimedia = $this->Multimedia->patchEntity($multimedia, $this->request->data); 

     $file = $_FILES['url']; 
     $path = 'files/' .$_FILES['url']['name']; 
     move_uploaded_file($this->data['url']['tmp_name'], $path); 



     if ($this->Multimedia->save($multimedia)) { 
      $this->Flash->success('The multimedia has been saved.'); 
      return $this->redirect(['action' => 'index']); 
     } else { 
      $this->Flash->error('The multimedia could not be saved. Please, try again.'); 
     } 
    } 
    $categories = $this->Multimedia->Categories->find('list', ['limit' => 200]); 
    $this->set(compact('multimedia', 'categories')); 
    $this->set('_serialize', ['multimedia']); 

} 

add.ctp

<?= $this->Form->create($multimedia, array('enctype' => 'multipart/form-data')); ?> 
<fieldset> 
    <legend><?= __('Add Multimedia') ?></legend> 
    <?php 
     echo $this->Form->input('title'); 
     echo $this->Form->input('description'); 
     echo $this->Form->input('mime_type'); 
     echo $this->Form->input('filename'); 
     echo $this->Form->input('url', array('type' => 'file')); 

     echo $this->Form->input('category_id', ['options' => $categories]); 
     echo $this->Form->input('created_by'); 

    ?> 
</fieldset> 
<?= $this->Form->button(__('Submit')) ?> 
<?= $this->Form->end() ?> 

我是新來的CakePHP 3,有點失落。請幫忙。

更新

已經取得保存圖像:

$file = $_FILES['url']; 
     $path = "webroot\\files\\" .$_FILES['url']['name']; 
     print_r($file); 
     move_uploaded_file($_FILES['url']['tmp_name'], $path); 

     if ($this->Multimedia->save($multimedia)) { 
      $this->Flash->success('The multimedia has been saved.'); 
      return $this->redirect(['action' => 'index']); 
     } else { 
      $this->Flash->error('The multimedia could not be saved. Please, try again.'); 
     } 

但現在的 「URL」 不保存在數據庫中

回答

0

我希望這可以幫助您,腳本這裏有一些改進,如使用陣列的短減速度:

的控制器編碼:

//don't forget to import the Folder class 
use Cake\Filesystem\Folder; 

public function add() 
{ 
    $multimedia = $this->Multimedia->newEntity(); 
    if ($this->request->is('post')) { 
      $multimedia = $this->Multimedia->patchEntity($multimedia, $this->request->data); 

      //upload script start here 
      $mm_dir = new Folder(WWW_ROOT . 'upload_dir', true, 0755); 
      $target_path = $mm_dir->pwd() . DS . $this->request->data('url.name'); 
      move_uploaded_file($this->request->data('url.tmp_name'), $target_path); 

      //save the file name in the field 'url' 
      $multimedia->url= $this->request->data('url.name'); 
      //the script ends here 

      if ($this->Multimedia->save($multimedia)) { 
        // ............ 
      } else { 
        // ......... 
      } 
    } 
    // .......... 
} 

add.ctp //改進

<?= $this->Form->create($multimedia, ['type' => 'file']); ?> 
echo $this->Form->input('url', ['type' => 'file']);