3

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 ?

Lemon Kazi
  • 3,308
  • 2
  • 37
  • 67

2 Answers2

5

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.....';  
}
Lemon Kazi
  • 3,308
  • 2
  • 37
  • 67
0

Pass filepath as first argument of unlink():

unlink(WWW_ROOT . 'media/' . $data['News']['image_url']['name'] . '/' . $data['News']['image_url']['tmp_name']);

Also make sure that you have proper permissions to perform this operation in directory containing image.

marian0
  • 3,336
  • 3
  • 27
  • 37