无法使用取消链接删除图像,是我的权限错误吗


Cannot remove image using unlink, are my permissions wrong?

我正试图使用取消链接来删除已删除评论中的图片,但它根本不起作用。数据库中的评论被删除,但实际图片没有。我做错了什么?文件夹权限为755,图像权限为644。

if (loggedin()) {
    $dblink = mysqli_connect($DBhostname, $DBusername, $DBpassword, $DBname);
    if (!$dblink) {die("Connection error (".mysqli_connect_errno.") " . mysqli_connect_error());}
    $commentid = mysqli_real_escape_string($dblink, $_GET['c']);
    $qry = "SELECT * FROM comments WHERE id='$commentid' LIMIT 1";
    $result = mysqli_query($dblink, $qry) or die(mysqli_error($dblink));
    if (mysqli_num_rows($result) > 0) {
        $row = mysqli_fetch_assoc($result);
        $commenter = $row["commenter"];
        $thereisimg = $row["thereisimg"];
        $imgtype = $row["imgtype"];
        // if logged in email = email of commenter
        if ($_SESSION["logged-in"] == $commenter) {
            // delete comment
            $qry = "DELETE FROM comments WHERE id=$commentid";
            $result = mysqli_query($dblink, $qry) or die(mysqli_error($dblink));
            // if image, delete image
            if ($thereisimg) {
                // delete image
                $imglink = "/imgs/commentpics/".$commentid.".".$imgtype;
                echo $imglink;
                unlink($imglink);
            }
        }
    }
}

要进行诊断,请尝试以下操作之一:

  1. 在PHP代码中添加错误处理程序以捕获错误
  2. 使用strace跟踪进程并获得unlink()系统调用的确切结果

以下是(1)的文档:http://www.w3schools.com/php/php_error.asp.

要做的2:

strace -e unlink php myScript.php

这假设脚本可以直接从命令行运行。

设置错误处理程序

<?php
function my_error_handler($error_level,$error_message,
$error_file,$error_line,$error_context) 
{
        echo $error_message;
}
set_error_handler("my_error_handler");
相关文章: