Cakephp:如何更新上传的图像
我忘了在表单标签中添加enctype=“multipart/form data” 我可以更新Cakephp:如何更新上传的图像,php,cakephp,Php,Cakephp,我忘了在表单标签中添加enctype=“multipart/form data” 我可以更新nameprice等,但我不知道如何更新图像。 共享我的上传和编辑代码 上传产品的控制器代码 public function products() { $this->loadModel("Product"); $this->loadModel("Subcat"); if(isset($this->request->data["b1"])) {
name
price
等,但我不知道如何更新图像。
共享我的上传和编辑代码
上传产品的控制器代码
public function products()
{
$this->loadModel("Product");
$this->loadModel("Subcat");
if(isset($this->request->data["b1"]))
{
$fn=time().$this->request->form['photo']['name'];
$filename=WWW_ROOT.DS.'images'.DS.'product'.DS.$fn;
move_uploaded_file($this->request->form['photo']['tmp_name'],$filename);
$this->request->data["photo"]=$fn;
if($this->Product->save($this->request->data))
{
$this->Session->setFlash('Successully save your information!');
}
}
$this->loadModel("Addcat");
$dt=$this->Addcat->find('list',array('fields'=>array('id','cat_name')));
$this->set('drop',$dt);
$this->loadModel("Subcat");
$dt1=$this->Subcat->find('list',array('fields'=>array('id','sub_cat')));
$this->set('drop1',$dt1);
$this->loadModel("Brand");
$dt2=$this->Brand->find('list',array('fields'=>array('id','name')));
$this->set('drop2',$dt2);
$this->loadModel("Product");
$this->layout="adminindex";
if(isset($this->request->query["action"]))
{
if($this->request->query["action"]=="delete")
{
$this->Product->delete($this->request->query["id"]);
}
}
$data = $this->Product->find('all');
$this->set('product',$data);
}
public function editproduct()
{
$this->loadModel("Product");
$this->layout="adminindex";
if(isset($this->request->query["id"]))
{
$data = $this->Product->findByid($this->request->query["id"]);
$this->set('product', $data);
}
if(isset($this->request->data["b1"]))
{
$fn=time().$this->request->form['photo']['name'];
$filename=WWW_ROOT.DS.'images'.DS.'product'.DS.$fn;
move_uploaded_file($this->request->form['photo']['tmp_name'],$filename);
$this->request->data["photo"]=$fn;
$this->Product->updateAll(
array("name"=>"'".$this->request->data["name"]."'",
"price"=>"'".$this->request->data["price"]."'",
"photo"=>"'".$this->request->data["photo"]."'",
"product_description"=>"'".$this->request->data["product_description"]."'"),
array('id'=>$this->request->data["id"]));
$this->redirect("../admin/products");
}
}
<div class="container">
<div class="row">
<div class="col-sm-4">
<div class="add category-form">
<h2>Edit Product!</h2>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $product["Product"]["id"]?>">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name" value="<?php echo $product["Product"]["name"]?>"></td>
</tr>
<tr>
<td>Price</td>
<td><input type="text" name="price" value="<?php echo $product["Product"]["price"]?>"></td>
</tr>
<tr>
<td>Photo</td>
<td><input type="file" name="photo" value="<?php echo $product["Product"]["photo"]?>"></td>
</tr>
<tr>
<td>Product Description</td>
<td><input type="text" name="product_description" value="<?php echo $product["Product"]["product_description"]?>"></td>
</tr>
<tr>
<td><button type="submit" class="b1" name="b1">Update</button></td>
</tr>
</table>
</form>
</div>
</div>
</div>
</div>
编辑产品的控制器代码
public function products()
{
$this->loadModel("Product");
$this->loadModel("Subcat");
if(isset($this->request->data["b1"]))
{
$fn=time().$this->request->form['photo']['name'];
$filename=WWW_ROOT.DS.'images'.DS.'product'.DS.$fn;
move_uploaded_file($this->request->form['photo']['tmp_name'],$filename);
$this->request->data["photo"]=$fn;
if($this->Product->save($this->request->data))
{
$this->Session->setFlash('Successully save your information!');
}
}
$this->loadModel("Addcat");
$dt=$this->Addcat->find('list',array('fields'=>array('id','cat_name')));
$this->set('drop',$dt);
$this->loadModel("Subcat");
$dt1=$this->Subcat->find('list',array('fields'=>array('id','sub_cat')));
$this->set('drop1',$dt1);
$this->loadModel("Brand");
$dt2=$this->Brand->find('list',array('fields'=>array('id','name')));
$this->set('drop2',$dt2);
$this->loadModel("Product");
$this->layout="adminindex";
if(isset($this->request->query["action"]))
{
if($this->request->query["action"]=="delete")
{
$this->Product->delete($this->request->query["id"]);
}
}
$data = $this->Product->find('all');
$this->set('product',$data);
}
public function editproduct()
{
$this->loadModel("Product");
$this->layout="adminindex";
if(isset($this->request->query["id"]))
{
$data = $this->Product->findByid($this->request->query["id"]);
$this->set('product', $data);
}
if(isset($this->request->data["b1"]))
{
$fn=time().$this->request->form['photo']['name'];
$filename=WWW_ROOT.DS.'images'.DS.'product'.DS.$fn;
move_uploaded_file($this->request->form['photo']['tmp_name'],$filename);
$this->request->data["photo"]=$fn;
$this->Product->updateAll(
array("name"=>"'".$this->request->data["name"]."'",
"price"=>"'".$this->request->data["price"]."'",
"photo"=>"'".$this->request->data["photo"]."'",
"product_description"=>"'".$this->request->data["product_description"]."'"),
array('id'=>$this->request->data["id"]));
$this->redirect("../admin/products");
}
}
<div class="container">
<div class="row">
<div class="col-sm-4">
<div class="add category-form">
<h2>Edit Product!</h2>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $product["Product"]["id"]?>">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name" value="<?php echo $product["Product"]["name"]?>"></td>
</tr>
<tr>
<td>Price</td>
<td><input type="text" name="price" value="<?php echo $product["Product"]["price"]?>"></td>
</tr>
<tr>
<td>Photo</td>
<td><input type="file" name="photo" value="<?php echo $product["Product"]["photo"]?>"></td>
</tr>
<tr>
<td>Product Description</td>
<td><input type="text" name="product_description" value="<?php echo $product["Product"]["product_description"]?>"></td>
</tr>
<tr>
<td><button type="submit" class="b1" name="b1">Update</button></td>
</tr>
</table>
</form>
</div>
</div>
</div>
</div>
编辑产品的视图
public function products()
{
$this->loadModel("Product");
$this->loadModel("Subcat");
if(isset($this->request->data["b1"]))
{
$fn=time().$this->request->form['photo']['name'];
$filename=WWW_ROOT.DS.'images'.DS.'product'.DS.$fn;
move_uploaded_file($this->request->form['photo']['tmp_name'],$filename);
$this->request->data["photo"]=$fn;
if($this->Product->save($this->request->data))
{
$this->Session->setFlash('Successully save your information!');
}
}
$this->loadModel("Addcat");
$dt=$this->Addcat->find('list',array('fields'=>array('id','cat_name')));
$this->set('drop',$dt);
$this->loadModel("Subcat");
$dt1=$this->Subcat->find('list',array('fields'=>array('id','sub_cat')));
$this->set('drop1',$dt1);
$this->loadModel("Brand");
$dt2=$this->Brand->find('list',array('fields'=>array('id','name')));
$this->set('drop2',$dt2);
$this->loadModel("Product");
$this->layout="adminindex";
if(isset($this->request->query["action"]))
{
if($this->request->query["action"]=="delete")
{
$this->Product->delete($this->request->query["id"]);
}
}
$data = $this->Product->find('all');
$this->set('product',$data);
}
public function editproduct()
{
$this->loadModel("Product");
$this->layout="adminindex";
if(isset($this->request->query["id"]))
{
$data = $this->Product->findByid($this->request->query["id"]);
$this->set('product', $data);
}
if(isset($this->request->data["b1"]))
{
$fn=time().$this->request->form['photo']['name'];
$filename=WWW_ROOT.DS.'images'.DS.'product'.DS.$fn;
move_uploaded_file($this->request->form['photo']['tmp_name'],$filename);
$this->request->data["photo"]=$fn;
$this->Product->updateAll(
array("name"=>"'".$this->request->data["name"]."'",
"price"=>"'".$this->request->data["price"]."'",
"photo"=>"'".$this->request->data["photo"]."'",
"product_description"=>"'".$this->request->data["product_description"]."'"),
array('id'=>$this->request->data["id"]));
$this->redirect("../admin/products");
}
}
<div class="container">
<div class="row">
<div class="col-sm-4">
<div class="add category-form">
<h2>Edit Product!</h2>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $product["Product"]["id"]?>">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name" value="<?php echo $product["Product"]["name"]?>"></td>
</tr>
<tr>
<td>Price</td>
<td><input type="text" name="price" value="<?php echo $product["Product"]["price"]?>"></td>
</tr>
<tr>
<td>Photo</td>
<td><input type="file" name="photo" value="<?php echo $product["Product"]["photo"]?>"></td>
</tr>
<tr>
<td>Product Description</td>
<td><input type="text" name="product_description" value="<?php echo $product["Product"]["product_description"]?>"></td>
</tr>
<tr>
<td><button type="submit" class="b1" name="b1">Update</button></td>
</tr>
</table>
</form>
</div>
</div>
</div>
</div>
编辑产品!
试试这个可能会有帮助
if($_REQUEST['action']=='Update'){
$cond="where emp_id='$emp_id'";
if(is_uploaded_file($_FILES["image"]["tmp_name"]))
{
$key = createActivationKey('5');
$img_name = $key."-".$_FILES["image"]["name"];
$path = "../view/img/teacher/";
move_uploaded_file($_FILES["image"]["tmp_name"], $path.$img_name);
$sourcepath = "../view/img/teacher/";
$targetpath = "../view/img/teacher/th/";
$targetpath2 = "../view/img/teacher/big/";
createThumb( $sourcepath.$img_name , $targetpath ,$img_name , '50' );
createThumb( $sourcepath.$img_name , $targetpath2 ,$img_name , '250' );
我只是忘了在表单标签中添加enctype=“multipart/form data”
您的图像在数据库中的文件夹和名称中了吗@Vkyno我的文件夹中没有图像,但数据库中图像更新的名称请从标题中删除已解决的问题,并将您的解决方案作为答案发布。