Did not have luck with these examples:
Deleting a File
There are no special permissions on the file.
Is there a way to do this in JQuery?
The requirement is – a certain file must be deleted from the web directory when another page is loaded. There is no security issue as this is on a closed network.
Any help is appreciated.
You can’t delete files over HTTP (well in theory you can, but it’s not implemented.)
In PHP the start would look like this: (Not expanding solution to a fully secure one because you’re not saying what platform you are on)
<? // STILL INSECURE!!!! // Do not use in any public place without authentication. // Allows deletion of any file within /my/files // Usage: filename.php?file=filename $basedir = "/my/files"; $file_to_delete = $_REQUEST["file"]; $path = realpath($basedir."/".$file_to_delete); if (substr($path, 0, strlen($basedir)) != $basedir) die ("Access denied"); unlink($path); ?>
you would call the script like this:
If you are doing this in a RESTFUL way, you would send an HTTP DELETE request.
jQuery’s ajax method states that you can use the method parameter to specify ‘DELETE’ but notes that some browsers may not support it.
Obviously you will need a webserver which will accept a DELETE request, and apply some sort of authentication/authorization so that joe random visitor can’t delete your files. I believe Apache’s mod_dav will get you started here.
But you may call some server page function that will delete file.
You need to use a server side language that has access to editing the files such as PHP, RoR, or ASP.
You can however use jQuery to call the server side code via AJAX such as $.get or $.post and then the server side code deletes it and it would seem as though JS is deleting the files.