我有以下代码来删除我网站上的帖子。此功能适用于视频帖子,它可以完美删除视频帖子。但它不适用于图像帖子,也不会从网络目录中删除帖子图像。我的代码是
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
删除故事工作正常,删除故事图像但删除帖子不删除图像。