PHP从DELETE请求中获取数据
问题描述:
我正在使用jquery插件进行多个文件上传。一切工作正常,除了删除图像。 Firebug说JS正在向函数发送DELETE请求。我如何从删除请求中获取数据?PHP从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的文件上传插件:link
答
一般情况下,如果DELETE请求,请求体中发送数据时,您可以通过读取数据使用以下代码:
$data = file_get_contents("php://input");
根据数据(通常JSON或形状编码)的编码,可以使用json_decode
或parse_str
将数据读入可用变量。
有关简单示例,请参阅this article,其中作者使用表单编码数据来处理PUT
请求。 DELETE
的作用相似。
你的情况不过,它看起来像文件名是从请求URL(调用$this->uri->segment(3);
)读取。当我看着你的代码,似乎变量$gll_id
不initailized,你不检查,如果结果对象$w
和变量$gll_name
是空的。也许这是导致删除失败。用ini_set("log_errors",1);
打开错误日志并查看您的服务器错误日志。如果取消链接失败,错误日志中应包含PHP试图取消链接路径 - 这是可能的路径是不正确的。
你需要表现出一定的代码。 – Jivings 2012-03-19 12:29:24