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

问题描述:

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

I'm trying to delete images by unlinking them via an AJAX function which encodes the filepath using encodeURIComponent before passing it.However, i am getting errors while unlinking them so i've done a bit of nosing around trying to find the error.

当我传递一个文件路径中有一个空格(例如... / test / test item.jpg)

When i'm passing a file path with a space in it(e.g .../test/test item.jpg)

我收到错误

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

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

However when i pass a filepath with no spaces in it (e.g ../test/testitem.jpg), i do not get any errors.Why am i getting an invalid argument when i pass an encoded filepath with spaces in it?I thought that by encoding it with encodeURIComponent, the spaces in the filepath should have been encoded and taken care of?

我尝试调用函数而不进行编码,当文件路径中包含空格时,我仍然会收到无效参数错误。我可以处理文件路径中的空格吗?

I've tried calling the functions without encoding, and i still only get the invalid argument error when the file path contains spaces in them.How would/should i handle the spaces in the filepaths?

我的功能:

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


我刚刚遇到类似的问题这是对我有用的

I just had similar problem and this is what works for me

unlink(urldecode($filepath));

有关urldecode的更多信息,请阅读本文: http://php.net/manual/en/function.urldecode.php

for more info on urldecode read this article: http://php.net/manual/en/function.urldecode.php