PHP从DELETE请求中获取数据(PHP get data from DELETE request)

编程入门 行业动态 更新时间:2024-10-27 21:16:37
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-File-Upload插件: link

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

最满意答案

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

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

根据数据的编码(通常是JSON或表单编码),您可以使用json_decode或parse_str将数据读入可用变量。

有关一个简单示例,请参阅此文章 ,其中作者使用表单编码数据来处理PUT请求。 DELETE工作方式类似。


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

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.

更多推荐

本文发布于:2023-07-20 06:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1195210.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   DELETE   PHP   data   request

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!