php删除文章时同时也删除对应图片

文章描述:

php删除一篇文章的时候也同时删除对应的封面图片

1、根据文章的ID获取到图片的路径

$thumb = 'upload/mysql-thumb-img.jpg';

2、使用@fopen来读取图片,判断图片是否存在,如果图片存在,使用unlink来删除指定路径的图片

if(@fopen($thumb,'r')) {
    echo $thumb;
    if(unlink($thumb)) {
        echo "true";
    }
}

 

发布时间:2021/07/27

发表评论