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

标签 javascript php jquery encoding

我正在尝试通过 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/

相关文章:

javascript - DataTables:按列对表进行排序,固定 i++ 值?

jquery - 更改日期时间选择器背景颜色

php - 从 MySQL Google Maps v3 上的表中删除标记

php - Laravel JWT token 在身份验证 JWT 方法中刷新后无效

jquery - 当鼠标下方的元素移动时,如何阻止 mousemove() 执行?

php - 尝试通过 Composer 在 Laravel 上安装 Ignition 时出现 "Installation failed, reverting ./composer.json to its original content."该怎么办?

php - 涉及mysql中两个表的搜索查询

javascript - 在 .then() 链中传递 Promise.join 处理程序的结果

javascript - 在 X 个结果后包装数据的 CSS 元素

javascript - 如何正确地将模板从 HTML5 UP 导入到 django