Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP get data from DELETE request

I am using jquery plugin for multiple file upload. Everything is working fine, except delete the images. Firebug say that JS it is sending DELETE request to the function. How can I get data from delete request?

PHP delete code:

public function deleteImage() {
    //Get the name in the url
        $file = $this->uri->segment(3);
    $r = $this->session->userdata('id_user');
    $q=$this->caffe_model->caffe_get_one_user($r);
        $cff_name= $q->name;
    $cff_id = $q->id_caffe;       

    $w = $this->gallery_model->gallery_get_one_user($gll_id);
    $gll_name = $w->name;
        $success = unlink("./public/img/caffe/$cff_name/$gll_name/" . $file);
        $success_th = unlink("./public/img/caffe/$cff_name/$gll_name/thumbnails/" . $file);

        //info to see if it is doing what it is supposed to 
        $info = new stdClass();
        $info->sucess = $success;
        $info->path = $this->getPath_url_img_upload_folder() . $file;
        $info->file = is_file($this->getPath_img_upload_folder() . $file);
        if (IS_AJAX) {//I don't think it matters if this is set but good for error checking in the console/firebug
            echo json_encode(array($info));
        } else {     //here you will need to decide what you want to show for a successful delete
            var_dump($file);
        }
    }

and JS is using jQuery-File-Upload plugin: link

like image 629
Sasha Avatar asked Mar 19 '12 12:03

Sasha


1 Answers

Generally, if the DELETE request sends data in the request body, you can read the data by using the following code:

$data = file_get_contents("php://input");

Depending on the encoding of the data (usually JSON or form-encoded), you use json_decode or parse_str to read the data into usable variables.

For a simple example, see this article, where the author uses form-encoded data to handle a PUT request. DELETE works similarly.


In your case however, it looks like the file name is read from the request URL (the call to $this->uri->segment(3);). When I look at your code, it seems that the variable $gll_id is not initailized and you don't check if the resulting object $w and the variable $gll_name are empty. Maybe this is causing the delete to fail. Turn on error logging with ini_set("log_errors",1); and have a look at your server error log. If the unlink fails, the error log should contain the path PHP tried to unlink - it's likely that the path is not correct.

like image 121
chiborg Avatar answered Oct 15 '22 23:10

chiborg