2

I tried to add a delete images function in my website, but when I try, I was prompt a blank page as it doesn't echo anything and it doesn't remove any file from my folder..

Image.php

<form action="deleteImage.php" method="post">
    <input name="delete_file" type="hidden" value="<?php echo $one_image["FILE_NAME"]?>">
    <input type="submit" value="Delete">
    </form>


deleteImage.php

if (array_key_exists('delete_file', $_POST)) {
$path = "images";
$filename =  $_POST['delete_file'];
    if (file_exists($filename)) {
        unlink($path . "/" . $filename);
        echo 'File ' . $filename . ' has been deleted';
    } else {
        echo 'Could not delete ' . $filename . ', file does not exist';
    }
}
3
  • You are checking if (file_exists($filename)), but trying to delete using unlink($path . "/" . $filename);? So you either need to add the $path . "/" . to your file_exists() -> if (file_exists($path . "/" . $filename)) or remove it from the unlink() -> unlink($filename);, depending on the file location Commented Jan 26, 2016 at 6:14
  • @Sean it's working, thanks! Commented Jan 26, 2016 at 6:20
  • Give me 30 seconds with this web page and I will wipe out all your images, and maybe your entire website. Filter your input. Commented Jan 26, 2016 at 6:34

1 Answer 1

1

try to run the following code

if (array_key_exists('delete_file', $_POST)) {
$path = "images";
$filename =  $path . "/" . $_POST['delete_file']; // build the full path here
    if (file_exists($filename)) {
        unlink($filename);
        echo 'File ' . $filename . ' has been deleted';
    } else {
        echo 'Could not delete ' . $filename . ', file does not exist';
    }
}else{
    echo 'error';
}

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

Comments

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.