I am creating simple blog and deleting post from database but I want to delete image posted with that post I have did some code for deleting post from database but need help to delete image with that specific post.
This is my Delete_post.php it's working fine to delete post from database help expected to delete image:
<?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>";
}
?>
This is what I tried to use:
<?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>";
}
?>
您可以在PHP中使用unlink()
删除文件
unlink('file path');
If you have the name of that file (best to have full path) just use unlink()
, follow documentation:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.