user2720360
user2720360

Reputation: 251

Cannot remove image using unlink, are my permissions wrong?

I'm trying to use unlink to delete pictures from deleted comments but it's simply not working. The comment from the database gets deleted but the actual picture doesn't. What am I doing wrong? The folder permissions are 755 and the image permissions are 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);
            }
        }
    }
}

Upvotes: 0

Views: 255

Answers (1)

ash
ash

Reputation: 5165

To diagnose, try one of the following:

  1. Add an error handler to the PHP code to capture the error
  2. Use strace to trace the process and get the exact result of the unlink() system call

Here's a document for (1): http://www.w3schools.com/php/php_error.asp.

To do 2:

strace -e unlink php myScript.php

That assumes the script can be run directly from the command line.

Setting an Error Handler

<?php

function my_error_handler($error_level,$error_message,
$error_file,$error_line,$error_context) 
{
        echo $error_message;
}

set_error_handler("my_error_handler");

Upvotes: 1

Related Questions