使用PHP排查目录中已使用文件的技术求助
Optimization Suggestions for Your Used File Scanner
Hey there! Let's tackle this problem step by step. Your current script is a good start, but it's missing some critical pieces that are causing it to miss used files. Here are the key optimizations and fixes to get you a complete list of actively used files in your root directory:
Key Issues in the Original Code
- Incorrect file paths: You're scanning
/public_html/but trying to read files with just their base name, which might not work if your script isn't running from that directory. - Missing PHP internal references: The script only looks at HTML tags, but PHP files often use
include,require, or their variants to reference other files—these are completely missed. - Inefficient array checks: Using
in_array()for every check slows things down as the list grows; using array keys is faster. - Unhandled DOM parsing warnings: Invalid HTML (common in real-world projects) can throw warnings that break parsing.
- Missing entry files: The script doesn't include the PHP files you're scanning themselves (e.g., an
index.phpthat's never referenced by another file but is the main entry point). - Limited tag coverage: It misses script sources, CSS links, and other resource references.
Optimized Code Implementation
<?php $rootDir = '/public_html/'; $usedFiles = []; // First, add all PHP files in root to the list (they're potential entry points) $files = scandir($rootDir); foreach ($files as $file) { $filePath = $rootDir . $file; // Skip directories and non-PHP files for initial entry list if (is_file($filePath) && pathinfo($filePath, PATHINFO_EXTENSION) === 'php') { $usedFiles[$file] = true; // Use keys for O(1) lookups } } // Now scan each used file for references foreach (array_keys($usedFiles) as $fileName) { $filePath = $rootDir . $fileName; $content = file_get_contents($filePath); if (!$content) continue; // 1. Parse HTML tags for references libxml_use_internal_errors(true); // Suppress HTML parsing warnings $dom = new DOMDocument(); $dom->loadHTML($content); libxml_clear_errors(); // Check <a> tags foreach ($dom->getElementsByTagName('a') as $tag) { $href = $tag->getAttribute('href'); $referencedFile = basename($href); $referencedPath = $rootDir . $referencedFile; if (is_file($referencedPath)) { $usedFiles[$referencedFile] = true; } } // Check <form> action attributes foreach ($dom->getElementsByTagName('form') as $tag) { $action = $tag->getAttribute('action'); $referencedFile = basename($action); $referencedPath = $rootDir . $referencedFile; if (is_file($referencedPath)) { $usedFiles[$referencedFile] = true; } } // Check <img> sources foreach ($dom->getElementsByTagName('img') as $tag) { $src = $tag->getAttribute('src'); $referencedFile = basename($src); $referencedPath = $rootDir . $referencedFile; if (is_file($referencedPath)) { $usedFiles[$referencedFile] = true; } } // Check <script> sources foreach ($dom->getElementsByTagName('script') as $tag) { $src = $tag->getAttribute('src'); if (!empty($src)) { $referencedFile = basename($src); $referencedPath = $rootDir . $referencedFile; if (is_file($referencedPath)) { $usedFiles[$referencedFile] = true; } } } // Check <link> stylesheets foreach ($dom->getElementsByTagName('link') as $tag) { $href = $tag->getAttribute('href'); if ($tag->getAttribute('rel') === 'stylesheet' && !empty($href)) { $referencedFile = basename($href); $referencedPath = $rootDir . $referencedFile; if (is_file($referencedPath)) { $usedFiles[$referencedFile] = true; } } } // 2. Parse PHP include/require references $phpPattern = '/(include|require)(_once)?\s*[\'\"](.*?)[\'\"]/i'; preg_match_all($phpPattern, $content, $matches); foreach ($matches[3] as $includePath) { $referencedFile = basename($includePath); $referencedPath = $rootDir . $referencedFile; if (is_file($referencedPath)) { $usedFiles[$referencedFile] = true; } } } // Output the list of used files print_r(array_keys($usedFiles)); ?>
Additional Notes
- Relative paths: The code uses
basename()since you only care about the root directory, but if you had subdirectories to consider, you'd need to resolve relative paths properly (e.g., usingrealpath()). - Dynamic references: This script won't catch dynamically generated paths (e.g.,
include $var . '.php'). For those, you'd need more advanced static analysis, but this covers most common cases. - Test first: Before deleting anything, cross-verify the list with your knowledge of the project—some files might be used in cron jobs, API endpoints, or other places not captured by this scan.
内容的提问来源于stack exchange,提问作者Gabor
相关产品推荐
相关产品推荐

