Cakephp:如何更新上传的图像

时间:2016-03-18 04:33:02

标签: php cakephp

我忘记在表单标记中添加enctype =“multipart / form-data”

我可以更新nameprice等。但我不知道如何更新图像。 分享我的代码以便上传和编辑。

上传产品的控制器代码

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>

2 个答案:

答案 0 :(得分:0)

尝试这一次可能会发现

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' );

答案 1 :(得分:0)

我忘了在表单标记中添加enctype="multipart/form-data"