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」 不保存在數據庫中