在cakephp中上载项目中的图像时,在该项目中保存项目时出错
我第一次在cakephp工作。我能够从一个有图像变量的产品模型开始。我可以上传图片,但之后我无法保存到产品。这是我的add.ctp在cakephp中上载项目中的图像时,在该项目中保存项目时出错,cakephp,Cakephp,我第一次在cakephp工作。我能够从一个有图像变量的产品模型开始。我可以上传图片,但之后我无法保存到产品。这是我的add.ctp <div class="products form"> <?php echo $this->Form->create('Product', array('enctype' => 'multipart/form-data')); ?> <fieldset> <legend><
<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>
有人知道为什么它会上传和移动图像,但不保存产品吗?
此外,这里是调试生成的内容($this->request->data)
另外一条可能有用的信息是,当我使用此代码时:
<?php echo $this->Form->create('Product', array('enctype' => 'multipart/form-data')); ?>
<?php echo $this->Form->create('Product'); ?>
它将允许正确上传图像,但不会保存产品,当我使用此代码时:
<?php echo $this->Form->create('Product', array('enctype' => 'multipart/form-data')); ?>
<?php echo $this->Form->create('Product'); ?>
它将保存产品,但不会上载图像在移动图像之前需要保存数据,请尝试此操作
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;
}
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"];
}
}
希望对你有用
下面是一个我以前如何实现文件上传的示例,尝试使用它作为指导,我建议保持简单,如果你想限制文件类型,我建议在模型中这样做
public function add() {
if($this->request->is('post')){
$file = $this->request->data['Upload']['file'];
if($this->Upload->save($this->data) && move_uploaded_file($file['tmp_name'],APP.'webroot/files/uploads'.DS.$this->Upload->id.'.mp4'))
{
$this->Session->setFlash('<p class="uploadflash">The upload has been saved</p>', true);
$this->redirect(array('controller'=>'Uploads', 'action' => 'watch', $this->Upload->id));
} else {
$this->Session->setFlash('<p class="loginerror">The upload could not be saved, mp4 files can be saved only.</p>', true);
}
}
}
公共函数添加(){
如果($this->request->is('post')){
$file=$this->request->data['Upload']['file'];
如果($this->Upload->save($this->data)和移动上传的文件($file['tmp\u name',APP'webroot/files/uploads'.DS.$this->Upload->id..mp4'))
{
$this->Session->setFlash(“上传已保存,true);
$this->redirect(数组('controller'=>'Uploads','action'=>'watch',$this->Upload->id));
}否则{
$this->Session->setFlash(“
无法保存上载,只能保存mp4文件。
”,true);
}
}
}
在移动图像之前需要保存数据,请尝试此操作
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;
}
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"];
}
}
希望对你有用
下面是一个我以前如何实现文件上传的示例,尝试使用它作为指导,我建议保持简单,如果你想限制文件类型,我建议在模型中这样做
public function add() {
if($this->request->is('post')){
$file = $this->request->data['Upload']['file'];
if($this->Upload->save($this->data) && move_uploaded_file($file['tmp_name'],APP.'webroot/files/uploads'.DS.$this->Upload->id.'.mp4'))
{
$this->Session->setFlash('<p class="uploadflash">The upload has been saved</p>', true);
$this->redirect(array('controller'=>'Uploads', 'action' => 'watch', $this->Upload->id));
} else {
$this->Session->setFlash('<p class="loginerror">The upload could not be saved, mp4 files can be saved only.</p>', true);
}
}
}
公共函数添加(){
如果($this->request->is('post')){
$file=$this->request->data['Upload']['file'];
如果($this->Upload->save($this->data)和移动上传的文件($file['tmp\u name',APP'webroot/files/uploads'.DS.$this->Upload->id..mp4'))
{
$this->Session->setFlash(“上传已保存,true);
$this->redirect(数组('controller'=>'Uploads','action'=>'watch',$this->Upload->id));
}否则{
$this->Session->setFlash(“
无法保存上载,只能保存mp4文件。
”,true);
}
}
}
尝试更改此行:
$this->data["Product"]["image"] = $this->data["Image"]["image"]["name"];
为此:
$this->request->data["Product"]["image"] = $this->request->data["Image"]["image"]["name"];
在原始代码中,您试图操作$this->data
,在保存调用中,您正在引用$this->request->data
:
$this->Product->save($this->request->data)
希望这会解决问题,如果没有错误本身,很难判断:)尝试更改这行:
$this->data["Product"]["image"] = $this->data["Image"]["image"]["name"];
为此:
$this->request->data["Product"]["image"] = $this->request->data["Image"]["image"]["name"];
在原始代码中,您试图操作$this->data
,在保存调用中,您正在引用$this->request->data
:
$this->Product->save($this->request->data)
希望这样可以解决问题,如果没有错误,很难判断:)我已经尝试了我能想到的每一个订单。我可以保存产品或图像。永远不要两者都做。尝试这样做
if($this->Product->save($this->request->data)&&move\u upload\u file($this->request->data[“Product”][“image”][“image”][“tmp\u name”]”,WWW\u ROOT./img/uploads/“$this->data[“Product”][“image”][“name”])==true))
我添加了一个我以前在上面的评论中使用过的上传文件,希望我已经试过了我能想到的每一道菜。我可以保存产品或图像。永远不要两者都做。尝试这样做if($this->Product->save($this->request->data)&&move\u upload\u file($this->request->data[“Product”][“image”][“image”][“tmp\u name”]”,WWW\u ROOT./img/uploads/“$this->data[“Product”][“image”][“name”])==true))
我添加了一个我以前在上面的评论中使用过的上传文件,希望帮助扫描您详细说明这里的//错误
?您收到了什么错误或消息,只是没有保存吗?您使用的是什么版本的蛋糕?能否详细说明//此处的错误?您收到了什么错误或消息,只是没有保存?您使用的是什么版本的蛋糕?这就是问题所在。非常感谢,这就是问题所在。非常感谢你。