How to remove image from web root when editing and updating cakephp data

I am writing an edit function to update news information, remove the previous image from the web root, and insert a new image:

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 cannot delete the old image and insert the new image, how can I fix my function?

+3


source to share


2 answers


Your data cannot find existing data here. 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.....';  
}

      

+6


source


Pass the path to the file as the first argument unlink()

:

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

      



Also make sure you have the appropriate permissions to perform this operation in the directory containing the image.

0


source







All Articles