views:

175

answers:

7

Did not have luck with these examples:
Javascript File remove
Javascript FSO DeleteFile Method
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.

Thanks.

+3  A: 

You can't delete files over HTTP (well in theory you can, but it's not implemented.)

The easiest way is to set up a tiny server side script (e.g. in ASP or PHP) and to call that from JavaScript. The server side script needs the proper permissions to do the deletion, but otherwise there is no problem.

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:

http://yourserver/directory/delete_file.php?file=directory/filename
Pekka
I'd just like to reemphasise the warning in the code for anyone reading this quickly - this code lets you delete any file that the script has permission to. Don't ever ever upload this anywhere!
Rich Bradshaw
@Rich yeah. I added basic security (restriction to a hard-coded base directory.)
Pekka
+6  A: 

With pure JavaScript, it can't be done. Using an AJAX call to a server side script that deletes the file would work though.

code_burgar
+2  A: 

You cannot delete a file on a remote server using only JavaScript running in a visitor's browser. This must be done with a server-side script.

Mike Daniels
+3  A: 

Javascript cannot delete files, it is prevented as it would lead to HUGE security vulnerabilities. THose links are for ActiveX controls that are handled through JS. Use a server side language.

Dustin Laine
+1  A: 

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.

David Dorward
Don't forget .htaccess.
dclowd9901
+1  A: 

Javascript is a client side language. So you are not able to delete file on server directly. All examples that you provide may be used only for deleting files on your local machine but not into server.

But you may call some server page function that will delete file.

Anton
+1  A: 

You can't delete files with JavaScript as it is run locally. So, it doesn't even touch external files.

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.

Oscar Godson