PHP gets data from DELETE request

I am using jquery plugin to upload multiple files. Everything works fine except deleting images. Firebug says JS sends a DELETE function request. How can I get data from a delete request?

PHP remove 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 uses jQuery-File-Upload plugin: link

+4
source share
1 answer

Typically, if a DELETE request sends data to the body of the request, you can read the data using the following code:

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

Depending on the encoding of the data (usually JSON or encoded forms) you use json_decode or parse_str to read the data into the variables used.

For a simple example, see this article , where the author uses the data in the form to process the PUT request. DELETE works the same way.


In your case, however, it looks like the file name is being read from the request URL (calling $this->uri->segment(3); ). When I look at your code, it seems that the $gll_id variable is not initialized, and you are not checking whether the resulting object $w and the variable $gll_name empty. Perhaps this causes the deletion to fail. Enable error logging with ini_set("log_errors",1); and view the server error log. If the communication failure is disabled, the error log should contain the path that PHP tried to disconnect - most likely, this path is incorrect.

+9
source

Source: https://habr.com/ru/post/1402225/


All Articles