2

I'm trying to delete a file with jQuery's $.ajax and php. I have the following.

/js/whatever.js

$('.deleteimage').live('click', function() {
    $imagefile = 'http://domain.com/images/1/whatever.jpg';
    $imagethumb = 'http://domain.com/images/1/thumbnail/whatever.jpg';
    $.ajax({
        type: 'POST',
        data: {
            action: 'deleteimage',
            imagefile: $imagefile,
            imagethumb: $imagethumb,
        },
        url: 'script.php',
        success: function(msg) {
            alert(msg);
        }
    })
})

/php/script.php

<?php
    if($_GET["action"]=="deleteimage")
    {
        $imagefile = $_REQUEST['imagefile'];
        $imagethumb = $_REQUEST['imagethumb'];
        $imagefileend = '../images'.end(explode('images',$imagefile)); //This will get me the path to the image ../images/1/whatever.jpg without the domain which is the correct path to the file. I tried that path directly and it deleted the file. 
        $imagethumbend = '../images'.end(explode('images',$imagethumb));

        unlink($imagefileend);
        unlink($imagethumbend);
    }
?>

All path are correct. In firebug i see the post variables are being sent correctly to script.php, however files are not being deleted. What am i doing wrong.

Pinkie
  • 10,126
  • 22
  • 78
  • 124

1 Answers1

4

In jQuery you use POST but in PHP you use GET. Change to if($_POST["action"]=="deleteimage") and please don't use $_REQUEST but $_POST

matino
  • 17,199
  • 8
  • 49
  • 58
  • GREAT. That was exactly the problem. It's working now using POST. Do you know the reason why it wouldn't work with REQUEST. – Pinkie Oct 05 '11 at 07:33
  • This was what I said at the comment. This works because you are sending as POST. Also I suggest avoiding using $_REQUEST in anywhere in your script. – Arda Oct 05 '11 at 07:36
  • @Pinkie - http://stackoverflow.com/questions/2142497/whats-wrong-with-using-request – matino Oct 05 '11 at 07:54
  • Yes i see why request is not a good idea. Thanks for the link. – Pinkie Oct 05 '11 at 07:59