PHP unlink一直在失败

时间:2013-04-15 13:51:29

标签: php mysql unlink

我正在尝试编写一个脚本来上传脚本以从我的服务器中删除图像。我一直收到错误消息。任何人都可以发现此代码有什么问题吗?

// Delete image
if(isset($_GET['deleteImg']) && !empty($_GET['deleteImg']) && $_GET['deleteImg'] == true)
{
 // Get imagepath from database
 $result = mysql_query("SELECT image FROM frankkluytmans WHERE id=$id");
 $imageDeletePath = mysql_fetch_assoc($result);

 // Delete image from server
 if(unlink($imageDeletePath['image']))
 {
    // Continue if image has been reset in database
    if(mysql_query("UPDATE frankkluytmans SET `image`='' WHERE id=$id")){
        // once deleted, redirect back to the view page
        header("Location: index.php"); 
    }
 }
 else
 {
    ?>
    <script type="text/javascript">
        window.alert('This image could not be deleted.';
    </script>
    <?
 }
}

1 个答案:

答案 0 :(得分:1)

我猜您的错误位于path函数的unlink(),正如您所说的实际字段就像/gfx/image.png,它不像绝对路径那样对我说,纠正我如果我错了

要删除file并直接使用unlink(),您的脚本应与图片位于同一文件夹中。所以我认为最好为您的条目设置absolute path,如

$path_abs = ' /customers/d/8/e/frankkluytmans.nl/httpd.www/testsite/cms'; //is the `gfx` folder inside `cms` folder? if it is this will work otherwise you have to change
if(unlink($path_abs . $imageDeletePath['image']))