我目前正試圖取消鏈接上傳的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);
}
請分享你的PHP代碼。 'unlink()'只要正確定義就不會與相對路徑發生衝突。請記住,它們與** PHP文件**相關,而不是執行AJAX請求的JS文件。根據您收到的錯誤判斷,您指定的路徑不正確。 – BenM
realpath()在失敗時返回FALSE,例如如果該文件不存在。 (來自php.net/realpath)可能路徑不好。 –
@LajosVeres路徑是有效的,因爲我在調用函數之前使用它的src中的filepath顯示一個img。由於圖像能夠顯示,文件路徑應該有效嗎? –