Search code examples
phpcakephpunlink

How to remove image from web root when data edit and update by cakephp


I write a edit function to update news's info, delete previous image from web root and insert new image:

code is below:

       if(unlink($data['News']['image_url']['tmp_name'], WWW_ROOT . 'media/' . $data['News']['image_url']['name'])) //delete image from root and database
            {
                echo 'image deleted.....';  //success message
            }

I can't delete old image and insert new image,how can i correct my function ?


Solution

  • Here your data can not find existing data. use this code

    $data1 = $this->News->findById($newsid);
    $this->request->data = $data1;
    $directory = WWW_ROOT . 'media';
    if(unlink($directory.DIRECTORY_SEPARATOR.$data1['News']['image_url']))  
    {
        echo 'image deleted.....';  
    }