Deleting files in javascript

Is it possible to delete a file inside a directory just using javascript ?. I currently have an index.php index that displays the name of the files in one directory and a check box for each file name, and the delete button below. I want to remove all selected checkboxes after clicking the delete button. I do not use mysql here, just a simple php file that displays names. can someone show me how to delete selected files using javascript?

0
source share
5 answers

You cannot delete files using javascript for security reasons . Bad guys can delete your stytem files :( However, you can do this with a combination of a server language such as PHP, ASP.NET, etc., Using what is known as Ajax .

Note. Javascript goes on to add / bid on the server. Node JS is an example of this.

Comment based update:

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'; } } 
+4
source

You can use AJAX and delete files using PHP on the server. You cannot manipulate files in pure javascript.

+4
source

You need to implement the file deletion function in PHP based on the built-in unlink () function. Be careful! for example, you should read the list of file names and calculate an identifier for each of them, and your delete function will accept the identifiers and is NOT the true file name.

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

+1
source

To do this, you do not need JavaScript, but an HTML form that sends file names to a PHP script on the server that deletes the files. See this example .

0
source

In this case, use the ajax function or otherwise javascript in the onclick function to provide a submit action and refresh the entire page.

0
source

Source: https://habr.com/ru/post/1304202/


All Articles