Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete file from folder using javascript?

Tags:

javascript

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();
        }
    }
like image 384
Simranjeet Kaur Avatar asked Nov 01 '13 06:11

Simranjeet Kaur


1 Answers

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
          }
        });
    }
}
like image 91
zzlalani Avatar answered Oct 25 '22 13:10

zzlalani