首页 > 解决方案 > php无法删除图片

问题描述

我有以下代码可以从我的网站上删除帖子。此功能适用于视频帖子,它可以完美删除视频帖子。但它不适用于图片帖子,也不会从网络目录中删除帖子图片。我的代码是

function delete_post($id, $fromStory = false) {
    $post = get_post($id);
    if ($fromStory and !$post['is_story']) return true;
    if (!$fromStory and !can_edit_post($post)) return false;
    if ($post['images']) {
        $images = perfectUnserialize($post['images']);
        if($images) {
            foreach($images as $image) {
                delete_file(path($image));
            }
        }

        if ($post['video']) {
            delete_file(path($post['video']));
        }
}

此外,我还删除了下面给出的旧故事功能

function delete_old_stories() {
    $time = time() - (3600 * config('story-deleted-at', 24));
    $query = db()->query("SELECT id,post_id FROM story_posts WHERE time_created < $time ");
    while($fetch = $query->fetch(PDO::FETCH_ASSOC)) {
        db()->query("DELETE FROM story_posts WHERE id=?", $fetch['id']); //delete story posts
        delete_post($fetch['post_id'], true);
    }
    return true;
}

这是删除文件功能

function delete_file($path)
{
    $basePath = path();
    $basePath2 = $basePath . '/';

    if ($path == $basePath or $path == $basePath2) return false;
    if (is_dir($path) === true) {
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::CHILD_FIRST);

        foreach ($files as $file) {
            if (in_array($file->getBasename(), array('.', '..')) !== true) {
                if ($file->isDir() === true) {
                    rmdir($file->getPathName());
                } else if (($file->isFile() === true) || ($file->isLink() === true)) {
                    unlink($file->getPathname());
                }
            }
        }

        return rmdir($path);
    } else if ((is_file($path) === true) || (is_link($path) === true)) {
        return unlink($path);


    }

    return false;
}

对于每个故事,目录中的图像文件是

_1000_8f81946314be5cfe962480b96c7df11e.jpg

对于 Post,目录中的图像文件是

_1000_8f81946314be5cfe962480b96c7df11e.jpg
_600_8f81946314be5cfe962480b96c7df11e.jpg

删除故事工作正常,它会删除故事图像,但删除帖子不会删除图像。

标签: phpfilesystemsdelete-file

解决方案


推荐阅读