php - 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
删除故事工作正常,它会删除故事图像,但删除帖子不会删除图像。
解决方案
推荐阅读
- influxdb - INFLUXDB:从按时间排序的测量中选择数据,其中时间 = MAX(时间)
- java - 如何使用 Java 中的 Google API 服务 SDK 构建计算客户端
- python - TurtleGraphicsError:错误的颜色字符串:黑色
- r - 在 R 中按国家/地区抽样观测
- swift - 更新选项卡栏控制器中的 UIButton 值
- angular - 如何在Angular 9中从url中删除哈希
- r - 将 x[0] 转换为 0 而不是 R 中的 numeric(0)
- mongodb - MongoDb db.collection.find() 带有特殊字符
- java - Jersey 客户端 POST 请求正文大小限制
- pandas - 在 Spark 中使用 Pandas udf 与 Facebook 先知进行预测