从php中的文件夹中删除图像

时间:2016-01-26 06:10:31

标签: php

我尝试在我的网站上添加删除图片功能,但是当我尝试时,我提示空白页面,因为它没有回显任何内容,并且它不会从我的文件夹中删除任何文件。

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

1 个答案:

答案 0 :(得分:1)

尝试运行以下代码

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