PHP从DELETE请求中获取数据


PHP get data from DELETE request

我正在使用jquery插件进行多文件上传。除了删除图像外,一切都很好。Firebug说JS正在向函数发送DELETE请求。如何从删除请求中获取数据?

PHP删除代码:

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);
        }
    }

JS正在使用jQuery文件上传插件:链接

通常,如果DELETE请求在请求体中发送数据,则可以使用以下代码读取数据:

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

根据数据的编码(通常是JSON或表单编码),可以使用json_decodeparse_str将数据读取到可用的变量中。

有关一个简单的示例,请参阅本文,其中作者使用表单编码的数据来处理PUT请求。CCD_ 4的工作原理类似。


然而,在您的情况下,文件名似乎是从请求URL(对$this->uri->segment(3);的调用)中读取的。当我查看您的代码时,变量$gll_id似乎没有初始化,并且您没有检查结果对象$w和变量$gll_name是否为空。也许这是导致删除失败的原因。使用ini_set("log_errors",1);打开错误日志并查看服务器错误日志。如果取消链接失败,错误日志应该包含PHP试图取消链接的路径——很可能是路径不正确。