PBOOTCMS图片清理升级版
PBOOTCMS图片清理升级版,可以同时清理/static/upload和/uploads目录下的文件,找到/apps/admin/controller/system/ImageExtController.php文件,修改为以下代码:
<?php
namespace app\admin\controller\system;
use app\admin\model\content\CompanyModel;
use app\admin\model\content\ContentModel;
use app\admin\model\content\ContentSortModel;
use app\admin\model\content\LinkModel;
use app\admin\model\content\SiteModel;
use app\admin\model\content\SlideModel;
use app\admin\model\content\LabelModel;
use app\home\model\MemberModel;
use core\basic\Controller;
class ImageExtController extends Controller
{
private $companyModel;
private $contentSortModel;
private $contentModel;
private $linkModel;
private $memberModel;
private $siteModel;
private $slideModel;
private $LabelModel;
public function __construct()
{
$this->companyModel = new CompanyModel();
$this->contentSortModel = new ContentSortModel();
$this->contentModel = new ContentModel();
$this->linkModel = new LinkModel();
$this->memberModel = new MemberModel();
$this->siteModel = new SiteModel();
$this->slideModel = new SlideModel();
$this->LabelModel = new LabelModel();
}
public function index()
{
$this->display('system/extimage.html');
}
public function checkDataFile()
{
$count = 30;
$page = get('page') ? get('page') : 1;
$start = ($page - 1) * $count;
$dataArr = [];
$company = $this->companyModel->getImage();
$contentSort = $this->contentSortModel->getImage();
$content = $this->contentModel->getImage();
$link = $this->linkModel->getImage();
$member = $this->memberModel->getImage();
$site = $this->siteModel->getImage();
$slide = $this->slideModel->getImage();
$label = $this->LabelModel->getImage();
$resArr = array_merge_recursive($company, $contentSort, $content, $link, $member, $site, $slide, $label);
array_walk_recursive($resArr, function ($key1) use (&$dataArr) {
if (!empty($key1)) {
$dataArr[] = str_replace(SITE_DIR . SITE_DIR . '/', SITE_DIR . '/', DOC_PATH . SITE_DIR .$key1);
}
return $dataArr;
});
$dataArr = array_unique($dataArr);
// Get uploaded files from both directories
$fileArr = [];
// Get files from /static/upload/ directory (only images)
$staticUploadPath = DOC_PATH . STATIC_DIR . '/upload';
$staticFileList = get_dir($staticUploadPath);
$this->filterImageFiles($staticFileList, $fileArr);
// Get files from /uploads/ directory (only images)
$uploadsPath = DOC_PATH . '/uploads';
if (is_dir($uploadsPath)) {
$uploadsFileList = get_dir($uploadsPath);
$this->filterImageFiles($uploadsFileList, $fileArr);
}
// Compare files
$difference = array_diff($fileArr, $dataArr);
$pageList = array_slice($difference, $start, $count);
$http = is_https() ? 'https://' : 'http://';
foreach ($pageList as &$value) {
// 修正预览地址生成逻辑
$staticPath = str_replace(DOC_PATH, '', $value);
// 处理/uploads/路径的特殊情况
if (strpos($staticPath, '/uploads/') === 0) {
$staticPath = '/uploads' . substr($staticPath, 8); // 保留一个/uploads
}
$value = [
'real_path' => $value,
'static_path' => $http . $_SERVER['SERVER_NAME'] . $staticPath,
'update_time' => date('Y-m-d H:i:s', filemtime($value))
];
}
$jsonData = ['code' => 0,'msg'=> '', 'count' => count($difference),'data' => $pageList];
return json_encode($jsonData);
}
public function do_ext()
{
$type = post('type');
$list = post('list');
switch ($type){
case 0:
// Clean selected files
foreach ($list as $value){
$this->moveFileToBackup($value['real_path']);
}
break;
case 1:
// Clean all redundant files
// Get uploaded files from both directories
$fileArr = [];
// Get files from /static/upload/ directory (only images)
$staticUploadPath = DOC_PATH . STATIC_DIR . '/upload';
$staticFileList = get_dir($staticUploadPath);
$this->filterImageFiles($staticFileList, $fileArr);
// Get files from /uploads/ directory (only images)
$uploadsPath = DOC_PATH . '/uploads';
if (is_dir($uploadsPath)) {
$uploadsFileList = get_dir($uploadsPath);
$this->filterImageFiles($uploadsFileList, $fileArr);
}
// Query data
$dataArr = [];
$company = $this->companyModel->getImage();
$contentSort = $this->contentSortModel->getImage();
$content = $this->contentModel->getImage();
$link = $this->linkModel->getImage();
$member = $this->memberModel->getImage();
$site = $this->siteModel->getImage();
$slide = $this->slideModel->getImage();
$Label = $this->LabelModel->getImage();
$resArr = array_merge_recursive($company, $contentSort, $content, $link, $member, $site, $slide, $Label);
array_walk_recursive($resArr, function ($key1) use (&$dataArr) {
if (!empty($key1)) {
$dataArr[] = str_replace(SITE_DIR . SITE_DIR . '/', SITE_DIR . '/', DOC_PATH . SITE_DIR .$key1);
}
return $dataArr;
});
$dataArr = array_unique($dataArr);
// Compare files and perform file migration
$difference = array_diff($fileArr, $dataArr);
foreach ($difference as $path) {
$this->moveFileToBackup($path);
}
break;
}
json(1,'');
}
/**
* Filter only image files from the file list
*/
private function filterImageFiles($fileList, &$fileArr)
{
$imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'];
array_walk_recursive($fileList, function ($key) use (&$fileArr, $imageExtensions) {
$ext = strtolower(pathinfo($key, PATHINFO_EXTENSION));
if (in_array($ext, $imageExtensions)) {
$fileArr[] = $key;
}
return $fileArr;
});
}
/**
* Move file to appropriate backup directory based on its original path
*/
private function moveFileToBackup($filePath)
{
if (strpos($filePath, '/static/upload/') !== false) {
$backupPath = DOC_PATH . STATIC_DIR . '/backup/ImageExt';
$fileRelativePath = strstr($filePath, '/static/upload/');
$targetPath = $backupPath . str_replace('/static/upload/', '/', $fileRelativePath);
} elseif (strpos($filePath, '/uploads/') !== false) {
$backupPath = DOC_PATH . STATIC_DIR . '/backup/uploads';
$fileRelativePath = strstr($filePath, '/uploads/');
$targetPath = $backupPath . $fileRelativePath;
} else {
return; // Skip files not in either directory
}
$targetDir = dirname($targetPath);
check_dir($targetDir, true);
if (file_exists($filePath)) {
rename($filePath, $targetPath);
}
}
}另外为了建议删除或隐藏“清理所有冗余文件”,位置:/apps/admin/view/default/system/extimage.html
