How can I delete all the listed files using PHP? As shown in the code, it displays all the files in the folder with a “Delete” button next to each one… but how can I use the unlink function to delete the particular file when its “Delete” button is clicked?
function Deletion()
{
$files1 = scandir('upload/');
$ignore = array( 'cgi-bin', '.', '..');
foreach ($files1 as $file1)
{
if(!in_array($file1, $ignore))
{
echo '<div>
<img src="upload/' . $file1 . '" height="50px" width="50px" />
<input type="button" value="Delete" name="del"/><br>
<a href="upload/'.$loca.'/'.$file1.'" target="_BLANK"
style=" font-size:12px; color:#333;"><p >'.$file1.'</p></a>
</div>';
}
}
}
You would need to perform an action on the server side when the button is clicked. Use AJAX to make a GET request to a page on your server with the filename as a parameter and then run unlink on the GET parameter. I generally suggest jQuery to beginners in AJAX.
JavaScript:
PHP: