admin管理员组

文章数量:1313934

I'm trying to delete a file with ajax request:

javascript:

function deleteFile(file_path)
{
    var r = confirm("Sure?")
    if(r == true)
    {
        $.ajax({
          url: 'delete_file.php',
          data: {'file' : file_path },
          method: 'GET',
          success: function (response) {
             alert('Deleted!');

          },
          error: function () {
             alert('Not Deleted!');

          }
        });
    }
}

delete_file.php :

unlink($_GET['file']);

It returns true on succes,but the file is not deleted.

I'm trying to delete a file with ajax request:

javascript:

function deleteFile(file_path)
{
    var r = confirm("Sure?")
    if(r == true)
    {
        $.ajax({
          url: 'delete_file.php',
          data: {'file' : file_path },
          method: 'GET',
          success: function (response) {
             alert('Deleted!');

          },
          error: function () {
             alert('Not Deleted!');

          }
        });
    }
}

delete_file.php :

unlink($_GET['file']);

It returns true on succes,but the file is not deleted.

Share Improve this question asked Feb 4, 2015 at 8:58 Petru LebadaPetru Lebada 1,68211 gold badges47 silver badges67 bronze badges 6
  • does your php/server instance have read/write/modify permissions in the folder where you are trying to delete? – Tschallacka Commented Feb 4, 2015 at 9:00
  • Check your link path before unlink. May be you link was relative not absolute – Nes Commented Feb 4, 2015 at 9:00
  • You should probably go into Debug mode and check the content of the response variable - or just point your web browser at the path (delete_file.php?file..). Possbily PHP is giving you errors, such as permission denied, but still returning a 200 OK status code. – CompuChip Commented Feb 4, 2015 at 9:01
  • I will check and return with a review – Petru Lebada Commented Feb 4, 2015 at 9:03
  • There's a HTTP verb called DELETE. If you <3 semantics, use that over GET. – Jørgen R Commented Feb 4, 2015 at 9:03
 |  Show 1 more ment

2 Answers 2

Reset to default 5

Check the response in AJAX, Best is use JSON DATA to check the response:

// Default AJAX request type is GET so no need to define  
$.ajax({
      url: 'delete_file.php',
      data: {'file' : file_path },
      dataType: 'json', 
      success: function (response) {
         if( response.status === true ) {
             alert('File Deleted!');
         }
         else alert('Something Went Wrong!');
      }
    });

Do It like this in PHP:

// First Check if file exists
$response = array('status'=>false);

if( file_exists('FILE_PATH/FILENAME') ) {
    unlink('FILE_PATH/FILENAME');
    $response['status'] = true;
}

// Send JSON Data to AJAX Request
echo json_encode($response);

Make sure you are giving the plete path with filename to unlink() function

Try this you need to check file, give permission, then delete it

$filename = 'full absolute file path';
if(file_exists($filename)) {
    @chmod($filename, 0777);
    @unlink($filename);
    return true;
}

As there can be two issues either the file path is not correct or the file is not having permission.

With the above code both will be checked.

本文标签: javascriptDelete a file with ajax requestStack Overflow