How To Delete Image From Server In PHP 2021?
Each image’s display markup would contain a form 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>';
Use This code in same PHP file:
if (array_key_exists('delete_file', $_POST)) { $filename = $_POST['delete_file']; if (file_exists($filename)) { unlink($filename); echo 'File '.$filename.' has been deleted'; } else { echo 'Could not delete '.$filename.', file does not exist'; } } // existing code continues below...
You can elaborate on this by using javascript: instead of submitting the form, you could send an AJAX request. The server-side code would look rather similar to this.