Php unlink() returning errors with encoded

2019-09-01 12:49发布

问题:

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.

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

i get the error

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

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?

My function:

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

};

Edit:PHP Code

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

回答1:

The interval is something like a special char in the file names. You need to escape it in order to operate with the file. Try this

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


回答2:

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

unlink(urldecode($filepath));

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