Today we will learn how to delete image from folder in php inside your website. There are a few ways. One, the simplest, should be included in that form; When it submits you respond to the post data and upload the image using Unlink.

How to Delete Image From Folder in PHP:-

Each image’s display markup has something like this:

echo '<form method="post">';
echo '<input type="hidden" value="'.$file.'" name="delete_file" />';
echo '<input type="submit" value="Delete image" />';
echo '</form>';

You can describe this in detail using JavaScript: Instead of submitting a form, you can send an Ajax request. Server-side code will look similar to this.

if (array_key_exists('delete_file', $_POST)) {
  $filename = $_POST['delete_file'];
  if (file_exists($filename)) {
    echo 'File '.$filename.' has been deleted';
  } else {
    echo 'Could not delete '.$filename.', file does not exist';
// existing code continues below...

Disclaimer: This is not secure. An attacker may use this code to delete any file on your server. You must extend this display code to add some security measures, otherwise, you can expect bad things.