469,275 Members | 1,871 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

count files in a directory

Hello, Using PHP 4, is there a fucntion that counts the files in a
given directory? You can assume there are no sub-directories within
this directory and hence, no files within the sub-directories.

Thanks for any functions or one-liners. - Dave

Jul 23 '06 #1
4 30063
la***********@zipmail.com schreef:
Hello, Using PHP 4, is there a fucntion that counts the files in a
given directory? You can assume there are no sub-directories within
this directory and hence, no files within the sub-directories.

Thanks for any functions or one-liners. - Dave
Hi Dave,

You could use readdir to accomplish this.
Example:

<?php
function countFiles($strDirName)
{
if ($hndDir = opendir($strDirName))
{
$intCount = 0;
while (false !== ($strFilename = readdir($hndDir)))
{
if ($strFilename != "." && $strFilename != "..")
{
$intCount++;
}
}
closedir($hndDir);
}
else
{
$intCount = -1;
}

return $intCount;
}

echo countFiles("C:\\Temp");
?>

HTH.
Peter.

--
http://www.phpforums.nl
Jul 23 '06 #2
<comp.lang.php>
<la***********@zipmail.com>
<23 Jul 2006 06:48:02 -0700>
<11*********************@75g2000cwc.googlegroups.c om>
Hello, Using PHP 4, is there a fucntion that counts the files in a
given directory? You can assume there are no sub-directories within
this directory and hence, no files within the sub-directories.

Thanks for any functions or one-liners.
<?php

$dirname="demo";
$dh=opendir($dirname);
while ($dave=readdir($dh))
{
print "$dave <br>";
}
closedir ($dh);

?>
--
Encrypted email address
www.emailuser.co.uk/?name=KRUSTOV
Make a shorter url
www.vhit.co.uk
Jul 23 '06 #3
<comp.lang.php>
<Krustov>
<Sun, 23 Jul 2006 15:54:51 +0100>
<MP***********************@news.newsreader.com>
<?php

$dirname="demo";
$dh=opendir($dirname);
while ($dave=readdir($dh))
{
print "$dave <br>";
}
closedir ($dh);

?>
I missed the bit where you said count .

<?php

$cnt=0;
$dirname="demo";
$dh = opendir($dirname);
while ($dave=readdir($dh))
{
print "$dave <br>";
$cnt=$cnt+1;
}
closedir ($dh);

print $cnt-2;

?>
--
Encrypted email address
www.emailuser.co.uk/?name=KRUSTOV
Make a shorter url
www.vhit.co.uk
Jul 23 '06 #4

la***********@zipmail.com wrote:
Hello, Using PHP 4, is there a fucntion that counts the files in a
given directory? You can assume there are no sub-directories within
this directory and hence, no files within the sub-directories.

Thanks for any functions or one-liners. - Dave
count(glob("$dir/*.*"));

Jul 23 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by rbt | last post: by
2 posts views Thread by Darren | last post: by
2 posts views Thread by Bruce | last post: by
3 posts views Thread by glub glub | last post: by
2 posts views Thread by MichiMichi | last post: by
4 posts views Thread by G E W hittle | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.