php 图片清理工具web版
该工具可以直接清理大于1mb的图片,扫描出来然后进行删除
访问该文件需要输入一个密码admin123 密码在文件里面可以自行修改
可以清理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>图片文件清理工具 © <?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>
把代码复制进去命名为del_images.php,或者别的名字都可以