gpt4 book ai didi

javascript - Php unlink() 返回包含空格的编码文件路径错误

转载 作者:行者123 更新时间:2023-12-02 17:54:46 26 4
gpt4 key购买 nike

我正在尝试通过 AJAX 函数取消链接来删除图像,该函数在传递文件路径之前使用encodeURIComponent对文件路径进行编码。但是,我在取消链接时遇到错误,所以我做了一些尝试来查找错误。

当我传递一个包含空格的文件路径时(例如.../test/test item.jpg)

我收到错误

PHP Warning:  unlink(): Invalid argument in ...//file location

但是,当我传递一个不带空格的文件路径(例如../test/testitem.jpg)时,我没有收到任何错误。为什么当我传递一个带空格的编码文件路径时,我得到一个无效的参数?我认为通过使用encodeURIComponent对其进行编码,应该对文件路径中的空格进行编码并处理?

我尝试在不编码的情况下调用函数,但当文件路径中包含空格时,我仍然只会收到无效参数错误。我将/应该如何处理文件路径中的空格?

我的功能:

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;
}

};

编辑:PHP 代码

$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);<--- This is the line returning the error
return ( true );
}
else
{
return ( false );
}

最佳答案

间隔类似于文件名中的特殊字符。您需要转义它才能操作该文件。试试这个

$filepath = str_replace(" ", "\ ", $filepath);
unlink($filepath);

关于javascript - Php unlink() 返回包含空格的编码文件路径错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21043884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com