[英]PHP count total files in directory AND subdirectory function
我需要在指定的目錄中獲得JPG文件的總數,包括它的所有子目錄。 沒有子目錄。
結構如下:
dir1/ 2 files subdir 1/ 8 files
總dir1 = 10個文件
dir2/ 5 files subdir 1/ 2 files subdir 2/ 8 files
總dir2 = 15個文件
我有這個功能,它不能正常工作,因為它只計算最后一個子目錄中的文件,總數是實際文件數量的2倍。 (如果我在最后一個子目錄中有40個文件,則輸出80)
public function count_files($path) {
global $file_count;
$file_count = 0;
$dir = opendir($path);
if (!$dir) return -1;
while ($file = readdir($dir)) :
if ($file == '.' || $file == '..') continue;
if (is_dir($path . $file)) :
$file_count += $this->count_files($path . "/" . $file);
else :
$file_count++;
endif;
endwhile;
closedir($dir);
return $file_count;
}
你可以使用RecursiveDirectoryIterator這樣做
<?php
function scan_dir($path){
$ite=new RecursiveDirectoryIterator($path);
$bytestotal=0;
$nbfiles=0;
foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) {
$filesize=$cur->getSize();
$bytestotal+=$filesize;
$nbfiles++;
$files[] = $filename;
}
$bytestotal=number_format($bytestotal);
return array('total_files'=>$nbfiles,'total_size'=>$bytestotal,'files'=>$files);
}
$files = scan_dir('./');
echo "Total: {$files['total_files']} files, {$files['total_size']} bytes\n";
//Total: 1195 files, 357,374,878 bytes
?>
為了它的樂趣,我把它鞭打在一起:
class FileFinder
{
private $onFound;
private function __construct($path, $onFound, $maxDepth)
{
// onFound gets called at every file found
$this->onFound = $onFound;
// start iterating immediately
$this->iterate($path, $maxDepth);
}
private function iterate($path, $maxDepth)
{
$d = opendir($path);
while ($e = readdir($d)) {
// skip the special folders
if ($e == '.' || $e == '..') { continue; }
$absPath = "$path/$e";
if (is_dir($absPath)) {
// check $maxDepth first before entering next recursion
if ($maxDepth != 0) {
// reduce maximum depth for next iteration
$this->iterate($absPath, $maxDepth - 1);
}
} else {
// regular file found, call the found handler
call_user_func_array($this->onFound, array($absPath));
}
}
closedir($d);
}
// helper function to instantiate one finder object
// return value is not very important though, because all methods are private
public static function find($path, $onFound, $maxDepth = 0)
{
return new self($path, $onFound, $maxDepth);
}
}
// start finding files (maximum depth is one folder down)
$count = $bytes = 0;
FileFinder::find('.', function($file) use (&$count, &$bytes) {
// the closure updates count and bytes so far
++$count;
$bytes += filesize($file);
}, 1);
echo "Nr files: $count; bytes used: $bytes\n";
您傳遞基本路徑,找到處理程序和最大目錄深度(-1表示禁用)。 找到的處理程序是您在外部定義的函數,它將從find()
函數中給出的路徑相對傳遞路徑名。
希望它有意義並幫助你:)
error_reporting(E_ALL);
function printTabs($level)
{
echo "<br/><br/>";
$l = 0;
for (; $l < $level; $l++)
echo ".";
}
function printFileCount($dirName, $init)
{
$fileCount = 0;
$st = strrpos($dirName, "/");
printTabs($init);
echo substr($dirName, $st);
$dHandle = opendir($dirName);
while (false !== ($subEntity = readdir($dHandle)))
{
if ($subEntity == "." || $subEntity == "..")
continue;
if (is_file($dirName . '/' . $subEntity))
{
$fileCount++;
}
else //if(is_dir($dirName.'/'.$subEntity))
{
printFileCount($dirName . '/' . $subEntity, $init + 1);
}
}
printTabs($init);
echo($fileCount . " files");
return;
}
printFileCount("/var/www", 0);
剛檢查過,它正在運行。 但結果的排列是不好的,邏輯是有效的
開發人員的答案實際上非常棒! 像這樣使用它來使它工作:
系統(“find.-type f -print | wc -l”);
如果有人想要計算文件和目錄的總數。
顯示/計算總目錄和子目錄數
find . -type d -print | wc -l
顯示/計算主目錄和子目錄中的文件總數
find . -type f -print | wc -l
僅顯示/計算當前目錄中的文件(無子目錄)
find . -maxdepth 1 -type f -print | wc -l
顯示/計算當前目錄中的總目錄和文件(無子目錄)
ls -1 | wc -l
每個循環的A可以更快地完成這個技巧;-)
我記得,opendir派生自SplFileObject類,它是一個RecursiveIterator,Traversable,Iterator,SeekableIterator類,因此,如果使用SPL標准PHP庫來檢索甚至在子目錄中的整個圖像計數,則不需要while循環。
但是,有一段時間我沒有使用PHP,所以我可能犯了一個錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.