jQuery Ajax和PHP取消链接

I am having a struggle with using jQuery to run an AJAX request which deletes files:

function deleteuploadedfile(filetodelete) {
             $imagefile = 'http://domain.co.uk/directory/uploads/'+filetodelete;
             alert($imagefile);
              $.ajax({
                type: 'POST',
                data: {
                    action: 'deleteimage',
                    imagefile: $imagefile,
                     },
            url: 'http://domain.co.uk/directory/delete.php',
            success: function(){
                alert('success');
              }
            })
        }


THE PHP FILE

<?php
    if($_POST["action"]=="deleteimage")
    {
        $imagefile = $_POST['imagefile'];
        $imagefileend = '/uploads/'.end(explode('images',$imagefile)); 
        unlink($_POST['imagefile']);
    }
?>

I am getting the alert 'success' But the files are not being removed from the server.

I have got this far, just need some guidance on what is happening and why the files are not being deleted.

In your PHP code, you are doing:

unlink($_POST['imagefile']);

$_POST['imagefile'] is coming from your ajax call, and being set in javascript to:

$imagefile = 'http://domain.co.uk/directory/uploads/'+filetodelete;
$.ajax({type: 'POST',
        data: {
              action: 'deleteimage',
              imagefile: $imagefile,
        }

How do you expect PHP to be able to delete a remote URL?

You seem to be setting up a $imagefileend variable with a local path, but you aren't using it in your unlink() call. Double check what you are trying to unlink(), spit out the full path right before you try to unlink it, and verify it exists locally and that you have appropriate permissions.

Also, unlink() returns true or false, based on success. Capture that and use it to help debug.

Suggested edit (untested):

 if($_POST["action"]=="deleteimage")
 {
     $imagefile = basename($_POST['imagefile']); // See http://php.net/basename
     $path = '/uploads/'. $imagefile;
     if(!file_exists($path)) {
          echo json_encode(array("success" => 0, "error" => "File $imagefile does not exist"));
          exit;
     }

     if(!unlink($path)) {
          echo json_encode(array("success" => 0, "error" => "File could not be deleted, check permissions"));
          exit;  
     }

     echo json_encode(array("success" => 1));   
 }

Then have a callback in your ajax function client side, check for the server response, and ensure that success = 1.

success: function(data){
    var response = $.parseJSON(data);
    if(!response.success) {
        alert(response.error);
    } else {
        alert("success!");
    }
}

Also, I'd recommend using Chrome (or Firefox) and watching the Network tab of Developer Tools so you can see exactly what the server is returning from the ajax call.

Since the PHP script itself is located and the request is carried out ok you will get success. What you would have to do to let your java script code know that something went wrong in your PHP script is to return some kind of error code for your java script to read and act upon.

Regarding you PHP script you try to unlink $_POST['imagefile'] which is a URL. This will not work so well. You should pass the local path on the webserver to the file you wish to delete.

Then return some code to your java script front end which indicates success or failure to delete the file and notify the user of that.