文件上传 CakePHP 3.4
File Upload CakePHP 3.4
我正在尝试从表单上传一个简单的文件以保存在我的 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,我不知道为什么。
非常感谢您的帮助。
山姆
你必须关心两件事
1.Proper 来自声明
添加enctype="multipart/form-data"
in your html form ( ['type' => 'file']
) in CakePHP form create to.那么您的代码将是
<?= $this->Form->create($productImage,['type' => 'file']) ?>
详情 Here
2。上传图片
以正确的方式上传图片
使用
$this->request->data['image']['name'];
我正在尝试从表单上传一个简单的文件以保存在我的 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,我不知道为什么。
非常感谢您的帮助。
山姆
你必须关心两件事
1.Proper 来自声明
添加enctype="multipart/form-data"
in your html form ( ['type' => 'file']
) in CakePHP form create to.那么您的代码将是
<?= $this->Form->create($productImage,['type' => 'file']) ?>
详情 Here
2。上传图片
以正确的方式上传图片
使用
$this->request->data['image']['name'];