删除不能在php函数中使用的帖子文件

时间:2018-04-30 17:14:19

标签: 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

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

我已经尝试了var_dump,它将消息作为字符串(94)

"/home3/myaim/abc.com/files/uploads/posts/photo/_%w_79b3d75a626fc66d67eee4ec9289‌​bd0a.jpg" Post deleted successfully 

但要删除的图像文件路径是字符串(94)

"/home3/myaim/abc.com/files/uploads/posts/photo/_1000_79b3d75a626fc66d67eee4ec92‌​89bd0a.jpg" Post deleted successfully 

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您可以使用unlink删除文件名。

示例:

unlink('imageText.jpg');