简体   繁体   中英

Cakephp : How to update uploaded image

i just forgot to add enctype="multipart/form-data" in form tag

i can update name . price etc. but i don't know how to update image. sharing my code for upload and edit.

Controller code for Upload product

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

}

Controller code for edit product

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");
        }

}

View for edit product

<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>

Try This One May HElp

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM