我试图从表单上传一个简单的文件以保存在我的 webroot/img 文件夹中,但是我遇到了一些困难。
这是视图中的表单:
<?= $this->Form->create($productImage) ?>
<fieldset>
<legend><?= __('Add Product Image') ?></legend>
<?php
echo $this->Form->control('product_id', ['options' => $products]);
echo $this->Form->file('image', ['label' =>'','size'=>'50']); //Uploaded file
echo $this->Form->control('image_path');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
这是控制器中的功能:
public function add()
{
$productImage = $this->ProductImages->newEntity();
if ($this->request->is('post')) {
$image = $this->request->data['image'];
if (move_uploaded_file($image,WWW_ROOT . '/img')) {
$this->Flash->success(__('file upload successful'));
} else {
$this->Flash->success(__('file upload unsuccessful'));
}
$productImage = $this->ProductImages->patchEntity($productImage, $this->request->getData());
if ($this->ProductImages->save($productImage)) {
$this->Flash->success(__('The product image has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The product image could not be saved. Please, try again.'));
}
$products = $this->ProductImages->Products->find('list', ['limit' => 200]);
$this->set(compact('productImage', 'products'));
$this->set('_serialize', ['productImage']);
}
move_uploaded_file 一直返回 false,我不知道为什么。
非常感谢您的帮助。
山姆。