Jump to content

php file count inc subdirectories


thebluebus

Recommended Posts

hi, wondered if you may be able to help me.

I'm trying to write a php script to count the number of files within a folder and its subdirectories

 

<?php
$count = 0;
foreach( glob( "images/*.*" ) as $filename ) {
$count++;
}
echo $count;
?>

 

but it only counts files in the one folder. How do i make it include subdirectories?

 

thanks  ;D

Link to comment
https://forums.phpfreaks.com/topic/97353-php-file-count-inc-subdirectories/
Share on other sites

Haven't tested this, but I think it'd work:

 

<?php

//$dir, a directory name ending with a "/"
function count_files ($dir)
{
$count = 0;
if(!is_dir($dir))
	return FALSE;

$h = opendir($dir);
while($file = readdir($h))
{
	if($file == "." || $file == "..")
		continue;
	if(is_dir($file)
		$count += count_files($dir.$file."/");
	else
		$count++;
}

closedir($h);

return $count;
}

?>

 

 

Orio.

It's a function... You call it with the dir's name:

 

<?php


$num_images = count_files("images/");
echo "There are ".$num_images." images!";



//$dir, a directory name ending with a "/"
function count_files ($dir)
{
$count = 0;
if(!is_dir($dir))
	return FALSE;

$h = opendir($dir);
while($file = readdir($h))
{
	if($file == "." || $file == "..")
		continue;
	if(is_dir($file)
		$count += count_files($dir.$file."/");
	else
		$count++;
}

closedir($h);

return $count;
}

?>

 

 

Orio.

I have another candidate that I just made up:

 

<?php
function count_deep($folder, $filetype = "*", $count_folders = false) {
$c = 0;
$dirs = array($folder);
while($dir = each($dirs)) {
	foreach(glob($dir[1]."/*", GLOB_ONLYDIR) as $filename) {
		$dirs[] = $filename;
	}
	$c += count(glob($dir[1]."/".$filetype));
}
if(!$count_folders && ($filetype == "*")) $c -= (count($dirs)-1);
return $c;
}

echo count_deep("images");
?>

I forgot to close a bracket:

 

<?php


$num_images = count_files("images/");
echo "There are ".$num_images." images!";



//$dir, a directory name ending with a "/"
function count_files ($dir)
{
$count = 0;
if(!is_dir($dir))
	return FALSE;

$h = opendir($dir);
while($file = readdir($h))
{
	if($file == "." || $file == "..")
		continue;
	if(is_dir($file))
		$count += count_files($dir.$file."/");
	else
		$count++;
}

closedir($h);

return $count;
}

?>

 

 

Orio.

ok, error is now gone, but the script is counting the number of files+folders in a directory and giving a number. e.g.

 

2 folders + 1 file = "There are 3 images"

 

I may have been unclear in my first post. I want it to count the total number of files in all the subdirectories within the specified directory

I have another candidate that I just made up:

 

<?php
function count_deep($folder, $filetype = "*", $count_folders = false) {
$c = 0;
$dirs = array($folder);
while($dir = each($dirs)) {
	foreach(glob($dir[1]."/*", GLOB_ONLYDIR) as $filename) {
		$dirs[] = $filename;
	}
	$c += count(glob($dir[1]."/".$filetype));
}
if(!$count_folders && ($filetype == "*")) $c -= (count($dirs)-1);
return $c;
}

echo count_deep("images");
?>

 

thanks a lot! works perfectly  ;D

Found my error. Works now.

 

<?php


$num_images = count_files("images/");
echo "There are ".$num_images." images!";



//$dir, a directory name ending with a "/"
function count_files ($dir)
{
$count = 0;
if(!is_dir($dir))
	return FALSE;

$h = opendir($dir);
while($file = readdir($h))
{
	if($file == "." || $file == "..")
		continue;
	if(is_dir($dir.$file))
		$count += count_files($dir.$file."/");
	else
		$count++;
}

closedir($h);

return $count;
}

?>

 

Orio.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.