0

I am trying to delete files on the SERVER by javascript, and I already read the tips from delete files by javascript

My part of javascript is current looks like that,

deleteFile = function() {
  return $.ajax({
    url: "delete.php"
  }).success(function(responce) {
    if (responce === 'deleted') {
      return alert("deleted");
    }
  });
};

And my "delete.php" is looks like that

<?php
  if unlink("data/hello.json")
     print "deleted"
?>

And I am using commend python3 -m http.server 8000 to setup server.

When I perform deletion, which run the function deleteFile(), and I got the following message in terminal, localhost - - [01/Dec/2015 17:19:17 "GET /delete.php HTTP/1.1" 200 -

That means the "delete.php" can be "GET" correctly, right? However, "data/hello.json" still exist, that means "delete.php" did not execute. I also check if "delete.php" is executable by using commend php -f delete.php , and it works, "data/hello.json" is deleted ! That means there should be no problem with my php script.

I am wondering where I went wrong.

Thanks

Community
  • 1
  • 1
jmzhang18
  • 141
  • 1
  • 9
  • 1
    Try the php like '' and execute it directly and see if it is working anyway. – Jonathan Dec 02 '15 at 23:20
  • Have you try absolute path to the file "data/hello.json"? And did you get this alert after AJAX call: return alert("deleted"); Also not sure if print "deleted" is good. – quarky Dec 02 '15 at 23:24
  • I tried , it works when I manually use commend "php -f delete.php" , but it does not work with ajax "GET" request from the javascript, even though I got the message "localhost - - [03/Dec/2015 10:19:17 "GET /delete.php HTTP/1.1" 200 -" – jmzhang18 Dec 03 '15 at 15:39

1 Answers1

0

This file could be accessed by some process and on Linux systems will be deleted when process closes this file.

In addition to that. You should not be using HTTP GET method for anything which changes data state. You can use POST or DELETE methods instead.

Also I don't see any user authorisation being done. Do you indend anyone to be able to delete you files?

Maksym Kozlenko
  • 10,273
  • 2
  • 66
  • 55
  • "POST" request may work, but the python server does not support "POST" request, might use other servers to do so. Since everything is in my local machine, everyone using this machine is able to delete the files. – jmzhang18 Dec 03 '15 at 15:41