2014-01-06 73 views
0

我目前正試圖取消鏈接上傳的PHP文件。爲此,我調用一個ajax函數,將相關文件路徑傳遞給另一個頁面,這會刪除文件。無法取消鏈接php中的文件

不過,我不斷收到錯誤

unlink(..filepath...) no such files or directory in C:..... 

在環顧四周,我發現,取消鏈接可能遇到的問題相對文件路徑,所以我嘗試使用

轉換相對filepahts以絕對的
$newfilepath=realpath($filepath); 

但是當我試圖呼應的真實路徑的結果,以檢查它是否成功執行,我得到的結果

bool(false) 

當我檢查了手冊,我看到

Note: 
The running script must have executable permissions on all directories in the 
hierarchy, otherwise realpath() will return FALSE. 

當文件被上載,它們被保存到使用chmod 0644的目錄,我的猜測是造成問題。如果是這樣,有什麼辦法可以解除鏈接文件?

功能:所謂

function DeleteImageDP(){ 

    var itemid=$('#DisplayDeleteItemID').val(); 
    var file=$('#DisplayDeleteFilePath').val(); 
    var filepath=encodeURIComponent(file); 
    var itempicid=$('#DisplayDeleteItemPicID').val(); 
    var cfm=confirm("Confirm deletion of picture? (Note: Picture wil be deleted permanently."); 
    if(cfm == true) 
    { 
     $.ajax({ 

     url:"delete/deletedp.php", 
     type:"POST", 
     data:"ItemID="+itemid+"&FilePath="+filepath+"&ItemPicID="+itempicid, 
     success:function(){ 

      alert("Image successfully deleted."); 
      $('#ImagePreviewDP').prop('src','').hide(); 
      $('#ImagePreviewDPValidate').val(''); 
      $('#DisplayDelete').hide(); 

      $('#ItemDetailsContainer').trigger('change'); 

     }, 
     error:function(){ 

      alert("Image could not be deleted due to an error."); 

     } 

     }); 
     return true; 
    } 
    else 
    { 
     return false; 
    } 

}; 

頁:

PS文件都被我的網站的用戶通過使用一種形式因此CHMOD

編輯上傳到我的服務器

$bizid=$_SESSION['BizID']; 
$itemid=$_POST['ItemID']; 
$file=$_POST['FilePath']; 
$filepath=realpath($file); 
$itempicid=$_POST['ItemPicID']; 
//empties dp field in items table 
$delete=$cxn->prepare("UPDATE `Items` SET `ItemDP`=:deleted WHERE `BusinessID`=:bizid AND `ItemID`=:itemid"); 
$delete->bindValue(":bizid",$bizid); 
$delete->bindValue(":itemid",$itemid); 
$delete->bindValue(":deleted","NULL"); 
$delete->execute(); 
//removes from itempics 
$deletepic=$cxn->prepare("DELETE FROM `ItemPics` WHERE `BusinessID`=:bizid AND `ItemID`=:itemid AND `ItemPicID`=:itempicid AND `FilePath` LIKE :search"); 
$deletepic->bindValue(":search","%DP"); 
$deletepic->bindValue(":bizid",$bizid); 
$deletepic->bindValue(":itemid",$itemid); 
$deletepic->bindValue(":itempicid",$itempicid); 
$deletepic->execute(); 

if($deletepic) 
{ 
    unlink($filepath); 
    return (true); 
} 
else 
{ 
    return (false); 
} 
+0

請分享你的PHP代碼。 'unlink()'只要正確定義就不會與相對路徑發生衝突。請記住,它們與** PHP文件**相關,而不是執行AJAX請求的JS文件。根據您收到的錯誤判斷,您指定的路徑不正確。 – BenM

+0

realpath()在失敗時返回FALSE,例如如果該文件不存在。 (來自php.net/realpath)可能路徑不好。 –

+0

@LajosVeres路徑是有效的,因爲我在調用函數之前使用它的src中的filepath顯示一個img。由於圖像能夠顯示,文件路徑應該有效嗎? –

回答

0

看起來您正在獲取相對於您的HTML頁面的URL,但處理刪除的實際PHP文件是一個目錄向下(即,在/delete/內)。

因此,您需要修改PHP端的相對URL以確保它找到正確的文件。

嘗試用下面的代碼更新您的PHP文件:

unlink('../'.$filepath); 
+0

感謝您清理up.I嘗試重構我的代碼,將某些文件移動到文件夾中,我忘記了文件位置已更改 –