0

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!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
kogh
  • 995
  • 4
  • 17
  • 30

3 Answers3

2

You already seem to have the mechanism for an AJAX call in place (presumably that's what loadLog does). 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 URL

http://my.server/delete.php?file=some.log

Your server-side script would then read the parameter and call unlink to 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).

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
0

Use the unlink function to delete a file. Presuming your var $rawlog is the path to the file:

unlink($rawlog);
Leopold Stotch
  • 1,452
  • 2
  • 13
  • 24
  • $log is actually what I want removed (the full path + filename), but how can I call that from a link in the loop? – kogh Dec 16 '11 at 23:31
0

Do you really want to unlink() the file on click?

Supposing the user decides that, actually, they don't want to delete the file and clicks the box again?

I suggest that you mark the file for deletion and then perform some scheduled cleanup once per week from cron or similar.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247