15

Is there any way to delete files from folder using javascript..? Here is my function

function deleteImage(file_name)
    {
        var r = confirm("Are you sure you want to delete this Image?")
        if(r == true)
        {
            var file_path = <?php echo dirname(__FILE__) . '/uploads/'?>+file_name;
            file_path.remove();
        }
    }
4
  • Let PHP delete the file using an AJAX call (do take care of authentication properly first). Commented Nov 1, 2013 at 6:09
  • Is it a file on a user's system, or one on a server? Commented Nov 1, 2013 at 6:09
  • Sir VINAY Kr. SHARMA m not here to fight with u..i didn't vote u -1..so please if u cant help me then dont answer here Commented Nov 1, 2013 at 6:22
  • I'm also not fighting with you! We are here to help each other. If you think I tried to fight with you, then I'm so sorry. Commented Nov 1, 2013 at 6:42

6 Answers 6

20

You cannot delete anything without any server-side script..

You can actually use ajax and call a server-side file to do that for e.g.

Make a file delete.php

<?php 
   unlink($_GET['file']);
?>

and in the javascript

function deleteImage(file_name)
{
    var r = confirm("Are you sure you want to delete this Image?")
    if(r == true)
    {
        $.ajax({
          url: 'delete.php',
          data: {'file' : "<?php echo dirname(__FILE__) . '/uploads/'?>" + file_name },
          success: function (response) {
             // do something
          },
          error: function () {
             // do something
          }
        });
    }
}
Sign up to request clarification or add additional context in comments.

Comments

4

You can not delete files with javascript for security reasons.However, you can do so with the combination of server-side language such as PHP, ASP.NET, etc using Ajax. Below is sample ajax call that you can add in your code.

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

4 Comments

is it possible to get the file_path in php... i m getting file name in javascript...can i get it in php within the same function?
i am from .net but this is very common scenario for any language, where u can access the file and remove the file...php.net/manual/en/function.pathinfo.php
my function works onclick event.. how can i use pathinfo() there?
You have to call java script code that mentioned above in onclick event...this java script code will make server side ajax call where u can use can php code...
4

Using NodeJS you can use filestream to unlink the file also.

It'd look something like this:

var fs = require('fs');
fs.unlink('path_to_your_file+extension', function (err) {
    //Do whatever else you need to do here
});

I'm fairly certain (although I havent tried it) that you can import the fs node_module in plain javascript but you'd have to double check.

If you cant import the module you can always download NPM on your machine, and

npm i fs

in some directory (command line) to get the javascript classes from that module to use on your markup page.

Comments

0

You can't do this. Actually JavaScript is sandboxed and it's not allowing to do such operations.

For deleting a file you need a server side scripting to achieve this. It depends on the fact that what server side language you are using to deal with.

2 Comments

then what should i do to delete file but not using php..iis there any way?
to delete the file you have to use a way which can access your physical directory and perform operation on it. SO the answer is you have to use server side scripting.
0

Javascript is a client side scripting language. If you want to delete files from server, use php instead.

Comments

0

You cannot do it by using javascript. But if the file resides in the server then you can use php to do that..you can use unlink in php.

unlink($path_to_file);

2 Comments

can i get the file_path in php using above function which i m using currently? becose m getting file name in javascript variable.. is it possible?
Since javascript is client side scripting and php is server side you cannot pass the javascript variable directly to php in the way that you have tried.You will have to pass the file name using a ajax call to a php function.

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.