14

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();
        }
    }
Simranjeet Kaur
  • 259
  • 1
  • 6
  • 18

6 Answers6

19

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
          }
        });
    }
}
zzlalani
  • 22,960
  • 16
  • 44
  • 73
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 !!');
    }
   }
  });
});
});
zzlalani
  • 22,960
  • 16
  • 44
  • 73
Kuldeep
  • 454
  • 1
  • 3
  • 11
  • 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? – Simranjeet Kaur Nov 01 '13 at 06:17
  • i am from .net but this is very common scenario for any language, where u can access the file and remove the file...http://php.net/manual/en/function.pathinfo.php – Kuldeep Nov 01 '13 at 06:25
  • my function works onclick event.. how can i use pathinfo() there? – Simranjeet Kaur Nov 01 '13 at 06:28
  • 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... – Kuldeep Nov 01 '13 at 06:30
3

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.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
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.

nrsharma
  • 2,532
  • 3
  • 20
  • 36
  • then what should i do to delete file but not using php..iis there any way? – Simranjeet Kaur Nov 01 '13 at 06:08
  • 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. – nrsharma Nov 01 '13 at 06:11
0

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

Pranav Kale
  • 629
  • 1
  • 5
  • 13
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);
Dilantha
  • 1,552
  • 2
  • 30
  • 46
  • 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? – Simranjeet Kaur Nov 01 '13 at 06:19
  • 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. – Dilantha Nov 01 '13 at 06:32