duanqiongchong0354 2014-01-10 12:02
浏览 47

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

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 );
}
  • 写回答

2条回答 默认 最新

  • dougangxin5695 2014-01-10 12:25
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动