使用php从images文件夹中删除图像

时间:2015-01-07 12:38:23

标签: php image delete-file

我正在创建简单的博客并从数据库中删除帖子但我想删除用该帖子发布的图片我已经做了一些代码来删除数据库中的帖子但需要帮助删除该特定帖子的图像。

这是我的Delete_post.php它可以正常删除期望删除图像的数据库帮助帖子:

<?php 
        include("includes/Config.php");

        if(isset($_GET['delete_post'])){

        $delete_id = $_GET['delete_post'];

        $delete_post = "DELETE FROM posts WHERE post_id='$delete_id' ";

        $run_delete = mysql_query($delete_post);


        echo "<script>alert('Post Has Been Deleted')</script>";
        echo "<script>window.open('../view_all_post.php','_self')</script>";
    }
?>

这是我试图使用的:

<?php 
        include("includes/Config.php");

        if(isset($_GET['delete_post'])){

        $delete_id = $_GET['delete_post'];

        $img_d = $_GET['img_id'];

        unlink("../../post_imgs/$img_d");

        $delete_post = "DELETE FROM posts WHERE post_id='$delete_id' ";

        $run_delete = mysql_query($delete_post);


        echo "<script>alert('Post Has Been Deleted')</script>";
        echo "<script>window.open('../view_all_post.php','_self')</script>";
    }
?>

enter image description here

2 个答案:

答案 0 :(得分:2)

您可以使用PHP中的unlink()删除文件

unlink('file path');

答案 1 :(得分:0)

如果您拥有该文件的名称(最好有完整路径),请使用unlink(),请按照文档:

http://php.net/manual/en/function.unlink.php