我第一次在 cakephp 工作。我能够开始使用具有图像变量的产品模型。我可以上传图片,但之后我无法保存到产品中。这是我的 add.ctp
<div class="products form">
<?php echo $this->Form->create('Product', array('enctype' => 'multipart/form-data')); ?>
<fieldset>
<legend><?php echo __('Add Product'); ?></legend>
<?php
echo $this->Form->input('name');
echo $this->Form->input('slug');
echo $this->Form->input('description');
echo $this->Form->input('price');
echo $this->Form->input('weight');
//upload image
echo $this->Form->input('Product.image', array('type'=>'file', 'tmp_name'=>'temp'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Products'), array('action' => 'index')); ?></li>
</ul>
</div>
这是我的 ProductsController add() 功能
public function add($seller_id = null) {
if ($this->request->is('post')) {
$this->Product->create();
debug($this->request->data);
if(isset($this->request->data["Product"]["image"]["name"]))
{
$file = new File($this->request->data["Product"]["image"]["name"]);
debug($this->request->data);
$ext = pathinfo(($this->request->data["Product"]["image"]["name"]), PATHINFO_EXTENSION);
if ($ext != 'jpg' && $ext != 'jpeg' && $ext != 'gif' && $ext != 'png')
{
$this->Session->setFlash('You may only upload image files.');
}else
{
if(move_uploaded_file($this->request->data["Product"]["image"]["tmp_name"],WWW_ROOT."/img/uploads/" . $this->data["Product"]["image"]["name"]) == true)
{
$this->data["Product"]["image"] = $this->data["Image"]["image"]["name"];
}
}
if ($this->Product->save($this->request->data)) //error here
{
$this->Session->setFlash(__('The product has been saved'));
$this->redirect(array('action' => 'index'));
$this->request->data['Product']['seller_id'] = $seller_id;
}
else {
$this->Session->setFlash(__('The product could not be saved. Please, try again.'));
}
}$this->Session->setFlash(__('The image could not be saved. Please, try again.'));
}
}
谁能看到为什么它会上传和移动图像但不保存产品?此外,这里是 debug($this->request->data);
array(
'Product' => array(
'name' => '8',
'slug' => '8',
'description' => '8',
'price' => '8',
'weight' => '8',
'image' => array(
'name' => 'celeb16.com-purple-strapless-short-bridesmaid-dress-g129-33(1).jpg',
'type' => 'image/jpeg',
'tmp_name' => 'C:\xampp\tmp\phpDD08.tmp',
'error' => (int) 0,
'size' => (int) 1404
)
)
)
另一条可能有用的信息是,当我使用此代码时:
<?php echo $this->Form->create('Product', array('enctype' => 'multipart/form-data')); ?>
它将允许图像正确上传,但不会保存产品,当我使用此代码时:
<?php echo $this->Form->create('Product'); ?>
它将保存产品但不上传图片