Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete the image from the server

Tags:

php

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.

like image 861
Muthusamy Avatar asked Apr 17 '26 05:04

Muthusamy


2 Answers

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.";
like image 150
Jai Shree Ganesh Avatar answered Apr 19 '26 21:04

Jai Shree Ganesh


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';
}
like image 20
A. Denis Avatar answered Apr 19 '26 21:04

A. Denis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!