Muthusamy
Muthusamy

Reputation: 306

Delete the image from the server

I used a unlink method to delete the image

This image is located in this file folder only.

$filename = "warning-icon.jpg";
  if (file_exists($filename)) {
    unlink($filename);
    echo 'File '.$filename.' has been deleted';
  } else {
    echo 'Could not delete '.$filename.', file does not exist';
  }

But the image is not deleted.

i have a server name called web04 that is my default server name (Client server name). I can't delete the images from the folder in this server. Same code i have checked in my system localhost, here image get deleted. But in web04 server, Image not get deleted.

Please help me. Thanks in Advance.

Upvotes: 3

Views: 461

Answers (3)

Muthusamy
Muthusamy

Reputation: 306

I have put a ticket to my web hosting team server. They gave a permission to delete the image. Now the image get deleted.

Thanks to all, those who are help me in this issue.

Upvotes: 0

Jai Shree Ganesh
Jai Shree Ganesh

Reputation: 511

define root directory:

$root_directory = '/home/myuser/htdocs';

Now delete the file:

if(unlink($root_directory.$_GET['file']))
    echo "File Deleted.";


 else
        echo "Couldn't delete the File.";

Upvotes: 4

A. Denis
A. Denis

Reputation: 562

As echo displayed "File name has deleted" and file still in directory, there are maybe another reasons like:

  • You are looking in another directory
  • You check file via browser, but browsers sometime cache it very hard
  • This file is repeatedly created again by some agent/event

Looks like unlink really delete file as you make "file_exists" check. Maybe try another check?

$filename = "warning-icon.jpg";
if (file_exists($filename)) {
  unlink($filename);
  echo 'File '.$filename.' has been deleted';
  if (file_exists($filename)) {
    echo "still exists!!!";
  }
} else {
  echo 'Could not delete '.$filename.', file does not exist';
}

Upvotes: 2

Related Questions