2012-03-19 125 views
4

我正在使用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

+0

你需要表现出一定的代码。 – Jivings 2012-03-19 12:29:24

回答

9

一般情况下,如果DELETE请求,请求体中发送数据时,您可以通过读取数据使用以下代码:

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

根据数据(通常JSON或形状编码)的编码,可以使用json_decodeparse_str将数据读入可用变量。

有关简单示例,请参阅this article,其中作者使用表单编码数据来处理PUT请求。 DELETE的作用相似。


你的情况不过,它看起来像文件名是从请求URL(调用$this->uri->segment(3);)读取。当我看着你的代码,似乎变量$gll_id不initailized,你不检查,如果结果对象$w和变量$gll_name是空的。也许这是导致删除失败。用ini_set("log_errors",1);打开错误日志并查看您的服务器错误日志。如果取消链接失败,错误日志中应包含PHP试图取消链接路径 - 这是可能的路径是不正确的。

+0

我有点失落(我从来没有与此合作过)。我如何从个人输入中获取数据? – Sasha 2012-03-19 12:42:41

+0

它的接缝的file_get_contents(“PHP://输入”)是不工作室内用ENCTYPE =“多部分/格式数据” o.O – Sasha 2012-03-19 13:04:00

+0

@Sasha我已经添加额外的解释。 – chiborg 2012-03-19 13:45:44