Basically I have a website that lists out the files in the directory above it, and I’m trying to make some kind of “x” link next to it and, upon clicked, it will delete said file from the server.
foreach($logs as $log)
{
$noext = str_replace(".html", "", $log);
$rawlog = str_replace("../", "", $noext);
echo "<li><a href='#' onClick='javascript:loadLog(" . $rawlog . ");LogLoop();'>".$rawlog."</a></li>";
}
I’m not sure how to approach it, could I perhaps use an onclick for the X which somehow calls a PHP function to delete said file?
Any help is greatly appreciated!
You already seem to have the mechanism for an AJAX call in place (presumably that’s what
loadLogdoes). So you would keep the same approach for deletion: make an AJAX call to the server, using the filename as a query variable, for example in the URLYour server-side script would then read the parameter and call
unlinkto delete the file.Be careful: You probably need to make sure only authenticated personnel can actually delete logs! Even then, Any scheme like the above can leave your user vulnerable to CSRF attacks (which is admittedly theoretical, but it could happen; see preventing csrf in php).