Question

Absolute path works with unlink():

<?php
     unlink('images/filename.jpg');
?>

But this does not work.

<?php
    $image_id = $_POST['image_id'];
    unlink('images/'.$image_id)
?>

File permission is ok, script can read, write and execute. Tried with single and double quotes it works with absolute file path but not with variables passed from $_POST[].

Any workaround here? Thank you.

Was it helpful?

Solution

may be clean up your query parameters a little bit. Not sure of it, but as I tried out my codes with multiple variations, it worked just perfect !

According to you,

unlink('images/filename.jpg');

is working perfectly. Then, I see no reason why the other code is not working.

do something like: $image_id = trim($_POST["image_id"]);

and before deleting the file, please check, whether the file exists or not, that way, you will be sure where the error lies in.

$image_url = "images/{$image_id}";
if(file_exists($image_url)){
    unlink($image_url);
} else {
    die('file does not exist');
}

OTHER TIPS

Try:

<?php
    $image_id = $_POST['image_id'];
    unlink('./images/'.$image_id)
?>

Adding the "./" before the images in the directory name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top