1

I've got a problem with deleting the uploaded images.

It inserts in the database and uploads just fine but it won't delete.

Example: I have two tables, modelImages and Model. In Model I save the path of one main image, but in modelImage there can be a lot of them. The tables are linked via modelID.

This is an example how the path looks like:img/1/1/someImageName.png.

(first 1 - makerID, second 1 - modelID).

include('connect.php'); //connect to database
$modelID=$_GET['modelID'];
$makerID=$_GET['makerID'];
$path="img/".$makerID."/".$modelID."/";

if ($stmt = $mysqli->prepare("SELECT images FROM modelImages WHERE modelID='$modelID'")) {

    $stmt->execute();    
    $stmt->bind_result($images);

    while ($stmt->fetch()) {
        if($images!=$path){
            unlink($images);
        }
    }

    $stmt->close();

}
else {
    printf("Prepared Statement Error: %s\n", $mysqli->error);
}

if ($stmt = $mysqli->prepare("SELECT mainImage FROM model WHERE makerID='$makerID' AND modelID='$modelID'")) {    

    $stmt->execute();
    $stmt->bind_result($mainImage);

    while ($stmt->fetch()) {
        if($mainImage!=$path){
            unlink($mainImage);
        }
    }

    $stmt->close();

}
else {
    printf("Prepared Statement Error: %s\n", $mysqli->error);
}

When I just use the first code , it deletes all the images from the modelImages table and if I just use the second code, it deletes the one image from the model table.

But if I use them together I get the unlink() error:

Warning: unlink(img/1/1/image1.png) [function.unlink]: No such file or directory in /home/...
Warning: unlink(img/1/1/image2.png) [function.unlink]: No such file or directory in /home/...
Warning: unlink(img/1/1/image3.png) [function.unlink]: No such file or directory in /home/...
Warning: unlink(img/1/1/image4.png) [function.unlink]: No such file or directory in /home/...

Conclusion: The unlink won't work if I use both of the querys together.

3
  • echo getcwd(); PS: you're in risk by accepting the direct user input as-is that is passed to unlink() Commented Feb 19, 2013 at 23:53
  • 1
    you're doing prepared statements all wrong. php.net/manual/en/mysqli-stmt.bind-param.php Commented Feb 19, 2013 at 23:59
  • The first code section already deletes all images in your path. That's why the second unlink(); throws warning. Commented Feb 20, 2013 at 0:03

1 Answer 1

1

Don't use relative paths unless a) you are absolutely sure, what the current work directory is (see getcwd()), or b) it is intended, that the path is located relative to difference work directories (like in workdirectory-aware CLI-tools). Always use, or create absolute paths

"/path/to/img/".$makerID."/".$modelID."/";
__DIR__ . "/../path/to/img/".$makerID."/".$modelID."/";

I would prefer the second one, because it is more portable.

Sign up to request clarification or add additional context in comments.

1 Comment

Using echo getcwd(); I got /home/mainSite/public_html/site and I changed my path to $path="/home/mainSite/public_html/site/img/".$proizvodacID."/".$modelID."/"; but it didn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.