2012-03-19 101 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