当前位置: 首页 > news >正文

php 图片清理工具web版

该工具可以直接清理大于1mb的图片,扫描出来然后进行删除

image

 访问该文件需要输入一个密码admin123   密码在文件里面可以自行修改

image

可以清理uploads里面所以子目录里面大于1m的图片

 以下是代码

<?php
// 安全措施:限制直接访问
if (php_sapi_name() === 'cli') {die("该脚本只能通过Web界面访问");
}// 配置设置
// $targetDir = __DIR__ . '/uploads';
$targetDir = __DIR__ ;
$maxSize = 1048576; // 1MB
$imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp'];
$password = 'admin123'; // 设置访问密码
$scanComplete = false;
$deleteComplete = false;
$largeImages = [];
$deletedFiles = [];
$errors = [];// 检查目录是否存在
if (!file_exists($targetDir) || !is_dir($targetDir)) {$errors[] = "错误:uploads目录不存在";
}// 身份验证
session_start();
if (!isset($_SESSION['authenticated'])) {if (isset($_POST['password']) && $_POST['password'] === $password) {$_SESSION['authenticated'] = true;} else if (isset($_POST['password'])) {$errors[] = "密码错误";}
}// 处理扫描请求
if (isset($_POST['scan']) && isset($_SESSION['authenticated'])) {$largeImages = findLargeImages($targetDir, $imageExtensions, $maxSize);$scanComplete = true;
}// 处理删除请求
if (isset($_POST['delete']) && isset($_SESSION['authenticated'])) {$filesToDelete = isset($_POST['files']) ? $_POST['files'] : [];$deletedFiles = deleteFiles($filesToDelete);$deleteComplete = true;// 重新扫描以更新列表$largeImages = findLargeImages($targetDir, $imageExtensions, $maxSize);
}// 查找大于指定大小的图片
function findLargeImages($dir, $extensions, $maxSize) {$largeImages = [];$dirIterator = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS);$iterator = new RecursiveIteratorIterator($dirIterator,RecursiveIteratorIterator::SELF_FIRST);foreach ($iterator as $file) {if (!$file->isFile()) continue;$filePath = $file->getPathname();$fileSize = $file->getSize();$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));if (in_array($extension, $extensions)) {if ($fileSize > $maxSize) {$largeImages[] = ['path' => $filePath,'size' => $fileSize,'relative_path' => str_replace(__DIR__ . '/', '', $filePath)];}}}return $largeImages;
}// 删除选中的文件
function deleteFiles($files) {$deletedFiles = [];foreach ($files as $file) {// 防止路径遍历攻击if (strpos($file, '../') !== false || !file_exists($file)) {continue;}if (unlink($file)) {$deletedFiles[] = $file;}}return $deletedFiles;
}// 格式化文件大小
function formatSize($bytes) {if ($bytes >= 1073741824) {return number_format($bytes / 1073741824, 2) . ' GB';} elseif ($bytes >= 1048576) {return number_format($bytes / 1048576, 2) . ' MB';} elseif ($bytes >= 1024) {return number_format($bytes / 1024, 2) . ' KB';} else {return $bytes . ' bytes';}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>图片文件清理工具</title><style>* {box-sizing: border-box;margin: 0;padding: 0;font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;}body {background-color: #f5f7fa;color: #333;line-height: 1.6;padding: 20px;}.container {max-width: 1200px;margin: 0 auto;background: #fff;border-radius: 10px;box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);overflow: hidden;}header {background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);color: white;padding: 30px 20px;text-align: center;}h1 {font-size: 2.5rem;margin-bottom: 10px;}.subtitle {font-size: 1.2rem;opacity: 0.9;max-width: 800px;margin: 0 auto;}.content {padding: 30px;}.card {background: #fff;border-radius: 8px;box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);padding: 25px;margin-bottom: 25px;}.card-title {font-size: 1.4rem;color: #2c3e50;margin-bottom: 20px;padding-bottom: 10px;border-bottom: 2px solid #f0f4f8;
        }.stats {display: grid;grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));gap: 20px;margin-bottom: 30px;}.stat-card {background: #f8f9fc;border-radius: 8px;padding: 20px;text-align: center;transition: transform 0.3s ease;}.stat-card:hover {transform: translateY(-5px);box-shadow: 0 6px 12px rgba(0, 0, 0, 0.08);}.stat-value {font-size: 2.5rem;font-weight: bold;color: #3498db;margin: 10px 0;}.stat-label {color: #7f8c8d;font-size: 1rem;}.btn {display: inline-block;background: #3498db;color: white;padding: 12px 25px;border: none;border-radius: 5px;cursor: pointer;font-size: 1rem;font-weight: 600;text-align: center;transition: all 0.3s ease;text-decoration: none;}.btn:hover {background: #2980b9;transform: translateY(-2px);box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);}.btn-danger {background: #e74c3c;
        }.btn-danger:hover {background: #c0392b;
        }.btn-success {background: #2ecc71;
        }.btn-success:hover {background: #27ae60;
        }.btn:disabled {background: #bdc3c7;cursor: not-allowed;}.file-list {margin: 25px 0;}.file-item {display: flex;align-items: center;padding: 15px;border-bottom: 1px solid #eee;transition: background 0.2s;}.file-item:hover {background: #f9fbfd;
        }.file-info {flex: 1;}.file-name {font-weight: 500;margin-bottom: 5px;}.file-size {color: #7f8c8d;font-size: 0.9rem;}.checkbox-container {margin-right: 15px;}.actions {display: flex;gap: 15px;margin-top: 25px;}.message {padding: 15px;border-radius: 5px;margin: 20px 0;}.error {background: #ffebee;color: #c62828;border: 1px solid #ffcdd2;
        }.success {background: #e8f5e9;color: #2e7d32;border: 1px solid #c8e6c9;
        }.info {background: #e3f2fd;color: #1565c0;border: 1px solid #bbdefb;
        }.login-form {max-width: 500px;margin: 50px auto;padding: 30px;}.form-group {margin-bottom: 20px;}label {display: block;margin-bottom: 8px;font-weight: 600;}input[type="password"] {width: 100%;padding: 12px 15px;border: 1px solid #ddd;border-radius: 5px;font-size: 1rem;}footer {text-align: center;padding: 20px;color: #7f8c8d;font-size: 0.9rem;border-top: 1px solid #eee;
        }@media (max-width: 768px) {.stats {grid-template-columns: 1fr;}.actions {flex-direction: column;}}</style>
</head>
<body><div class="container"><header><h1>图片文件清理工具</h1><p class="subtitle">扫描并删除uploads目录中大于1MB的图片文件</p></header><div class="content"><?php if (!isset($_SESSION['authenticated'])): ?><div class="card login-form"><h2 class="card-title">身份验证</h2><?php if (!empty($errors)): ?><div class="message error"><?php foreach ($errors as $error): ?><p><?php echo htmlspecialchars($error); ?></p><?php endforeach; ?></div><?php endif; ?><form method="post"><div class="form-group"><label for="password">请输入访问密码:</label><input type="password" id="password" name="password" required></div><button type="submit" class="btn">登录</button></form></div><?php else: ?><div class="stats"><div class="stat-card"><div class="stat-value"><?php echo count($largeImages); ?></div><div class="stat-label">大于1MB的图片</div></div><div class="stat-card"><div class="stat-value"><?php$totalSize = 0;foreach ($largeImages as $image) {$totalSize += $image['size'];}echo formatSize($totalSize);?></div><div class="stat-label">可释放空间</div></div><div class="stat-card"><div class="stat-value"><?php echo count($deletedFiles); ?></div><div class="stat-label">已删除文件</div></div></div><?php if (!empty($errors)): ?><div class="message error"><?php foreach ($errors as $error): ?><p><?php echo htmlspecialchars($error); ?></p><?php endforeach; ?></div><?php endif; ?><?php if ($deleteComplete && !empty($deletedFiles)): ?><div class="message success"><p>成功删除 <?php echo count($deletedFiles); ?> 个文件!</p></div><?php endif; ?><div class="card"><h2 class="card-title">文件扫描</h2><p>扫描uploads目录中所有大于1MB的图片文件(支持格式:<?php echo implode(', ', $imageExtensions); ?>)</p><form method="post"><button type="submit" name="scan" class="btn">开始扫描</button></form><?php if ($scanComplete): ?><?php if (empty($largeImages)): ?><div class="message success"><p>未找到大于1MB的图片文件!</p></div><?php else: ?><div class="message info"><p>找到 <?php echo count($largeImages); ?> 个大于1MB的图片文件</p></div><?php endif; ?><?php endif; ?></div><?php if (!empty($largeImages)): ?><div class="card"><h2 class="card-title">大文件列表</h2><p>选择要删除的文件:</p><form method="post" id="deleteForm"><div class="file-list"><?php foreach ($largeImages as $index => $image): ?><div class="file-item"><div class="checkbox-container"><input type="checkbox" name="files[]" value="<?php echo htmlspecialchars($image['path']); ?>" id="file<?php echo $index; ?>"></div><div class="file-info"><div class="file-name"><?php echo htmlspecialchars($image['relative_path']); ?></div><div class="file-size"><?php echo formatSize($image['size']); ?></div></div></div><?php endforeach; ?></div><div class="actions"><button type="button" id="selectAllBtn" class="btn">全选/取消全选</button><button type="submit" name="delete" class="btn btn-danger" onclick="return confirm('确定要删除选中的文件吗?此操作不可恢复!');">删除选中文件</button></div></form></div><?php endif; ?><?php endif; ?></div><footer><p>图片文件清理工具 &copy; <?php echo date('Y'); ?> | 安全提示:操作前请确保已备份重要文件</p></footer></div><script>// 全选/取消全选功能document.getElementById('selectAllBtn').addEventListener('click', function() {const checkboxes = document.querySelectorAll('input[type="checkbox"]');const allChecked = Array.from(checkboxes).every(checkbox => checkbox.checked);checkboxes.forEach(checkbox => {checkbox.checked = !allChecked;});this.textContent = allChecked ? '全选' : '取消全选';});// 删除前的确认提示document.getElementById('deleteForm').addEventListener('submit', function(e) {const checkedFiles = document.querySelectorAll('input[type="checkbox"]:checked');if (checkedFiles.length === 0) {alert('请至少选择一个文件进行删除');e.preventDefault();}});</script>
</body>
</html>
View Code

把代码复制进去命名为del_images.php,或者别的名字都可以

http://www.aitangshan.cn/news/775.html

相关文章:

  • 题解:洛谷 P5997 [PA 2014] Pakowanie
  • 【CAPL】自定义函数的四种类型
  • KubeSphere闭源风波下,Casibase容器云为何成为用户更迫切的需求?
  • 使用类正则语法创建spaCy匹配模式
  • (自适应手机端)水处理设备网站模板 净水设备网站源码下载
  • tray + tkinter
  • istio-Ingress 和 nginx-ingress 的差别
  • (自适应手机端)电气传感器pbootcms网站模板
  • 利用GNURadio让你听到Laurel和Yanny的声音
  • AI-Ready Data信息梳理
  • 题解:[GDCPC 2024] 图
  • 数字中国创新的底层密码:开源新基建
  • (自适应手机端)旅游博客网站模板 个人博客网站源码下载
  • 光隔离探头与传统探头的核心差异解析
  • 【译】Visual Studio 2015 停用:针对旧版本 Visual Studio 的支持提醒
  • 认证协议:OAuth 2.0 和 JWT的学习总结
  • (自适应手机端)厨余垃圾处理设备网站模板
  • mqtt+esp32公网控制PIn 2 led灯
  • 题解:P4350 [CERC2015] Export Estimate
  • Nouveau——第三方开源NVIDIA驱动
  • (自适应手机端)政府机构网站模板 组织协会网站源码下载
  • OpenCV入门(18):图像边缘检测
  • GNOME桌面自动隐藏顶栏
  • 文件已经删除但空间未释放排查记录
  • 用通俗的语言讲讲音频格式中的位深
  • (自适应手机端)家私家纺网站模板 床上用品网站源码下载
  • PKC7150 高频交直流电流探头在智能工厂电力监测项目中的应用方案
  • 夏夜星空 - Karry
  • (自适应手机端)中英文双语网站模板 电子元件科研芯片网站模板
  • (PC+WAP)实验室化学仪器设备网站模板