0

is it possible to delete a file(s) inside the directory by just using javascript ?. what I currently have is, an index.php which displays the name of the files in the same directory and and a checkbox to each file name and at the bottom is a delete button. what I want to happen is, delete all the selected checkboxes once the delete button is clicked. I am not using mysql here, just a plain php file that displays the names. can someone show me how to delete the selected files using javascript ?

5
  • 3
    Imagine the fun if JavaScript was able to do this natively and you'd visit a site that does a `rm -rf /' on your machine :-D. Actually, you can access the filesystem in JavaScript in browser-specific ways (such as through an ActiveX object in Internet Explorer). Commented Apr 19, 2010 at 7:40
  • @stakx I wouldn't call ActiveX a JavaScript browser-specific way... Commented Apr 19, 2010 at 8:18
  • @deceze: Why not? The code itself will still be written in JavaScript (or EcmaScript, if you like), BUT it'll be browser-specific. The EcmaScript standard defines certain standard objects (such as Date or Math), others such as window or location are part of the de facto Browser Object Model standard, and yet others -- such as the availability of certain ActiveX components -- are even more browser-specific. I don't see the problem. Commented Apr 19, 2010 at 8:38
  • @stakx But you would need to write the ActiveX component in something other than Javascript, so I'd rather call it a browser-specific way that is exploitable by Javascript. A matter of POV I guess. :) Commented Apr 19, 2010 at 8:45
  • @deceze: I didn't claim, nor intend to say, that ActiveX components must themselves be written in JavaScript (they aren't, of course). But you are perhaps correct in that my comment may be misleading to someone who doesn't know about ActiveX. Commented Apr 19, 2010 at 8:50

5 Answers 5

4

You can not delete files with javascript for security reasons. Bad guys can delete files of your stytem :( However, you can do so with the combination of server-side language such as PHP, ASP.NET, etc using what is know as Ajax.

Note: Javascript is moving to adding/becoming the server-side language options. Node JS is an example of that.

Update Based On Comment:

You can delete files something like this:

<a href="#" class="delete">Delete</a>

JQuery:

$(function(){
    $('a.delete').click(function(){
      $.ajax({
       url:'delete.php',
       data:'id/name here',
       method:'GET',
       success:function(response){
        if (response === 'deleted')
        {
           alert('Deleted !!');
        }
       }
      });
    });
});

PHP:

   if (isset($_GET['id/name here']))
   {
     if (unlink('your_folder_path' . $_GET['id/name here']))
     {
       echo 'Deleted';
     }
   }
Sign up to request clarification or add additional context in comments.

3 Comments

can you show me an example of that ajax thingy based on my problem's description ?
thank you very much for the example sir, i was thinking of using manual ajax, but it's hard for me LOL, jquery is the best.
@sasori: You should take this as an example only! Without additional security checks, this can get very dangerous, when the submitted file name is something like ../../etc/passwords (assumed the server would have write permissions, but still...).
4

You can use AJAX,and delete files using PHP on server. You can't manipulate with files in pure javascript.

1 Comment

can you show me an example of that ajax thingy based on my problem's description ?
1

You need to implement the file deletion function in PHP based on the built-in unlink() function. Be careful here!! for example, you should read the list of file names and calculate an ID for each of them, and your delete function would accept IDs and NOT the real filename(s).

Then, when you send the file list to the browser, it includes the generated IDs as hidden fields or object attributes, etc. From JavaScript, you can use an HTTP request to send a list of file IDs to be deleted, based on the checkboxes. Your PHP script would call your delete function for the IDs.

Comments

0

You don't need JavaScript for that but a HTML form which sends the filenames to a PHP script on the server which deletes the files. See this example.

Comments

0

In this, use ajax function or otherwise by javascript in onclick function provide the submit action and refresh the whole page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.