以下是一个用 PHP 编写的递归查看文件夹下文件数和代码行数的简单示例:
<?php
function countFilesAndLines($directory, &$totalFiles = 0, &$totalLines = 0) {
$files = scandir($directory);
foreach ($files as $file) {
if ($file == '.' || $file == '..') {
continue;
}
$filePath = $directory . DIRECTORY_SEPARATOR . $file;
if (is_dir($filePath)) {
countFilesAndLines($filePath, $totalFiles, $totalLines);
} elseif (is_file($filePath)) {
$totalFiles++;
$totalLines += count(file($filePath, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES));
}
}
}
$directoryPath = '/path/to/your/folder';
$totalFiles = 0;
$totalLines = 0;
countFilesAndLines($directoryPath, $totalFiles, $totalLines);
echo "Total files: $totalFiles\n";
echo "Total lines of code: $totalLines\n";
请将 /path/to/your/folder
替换为你要检查的文件夹路径。这个脚本会递归遍历文件夹,统计文件数和代码行数。