abc123
abc123

Reputation: 33

delete images from folder in php

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';
    }
}

Upvotes: 2

Views: 7302

Answers (1)

suibber
suibber

Reputation: 267

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';
}

Upvotes: 1

Related Questions