|
Posted by Aaron Saray on 01/15/08 03:12
On Jan 13, 7:59 pm, Fred Atkinson <fatkin...@mishmash.com> wrote:
> On Sun, 13 Jan 2008 19:32:22 -0500, Daniel Ennis
>
>
>
> <dan...@fanetworks.net> wrote:
> >Did you try my solution? It should be alot quicker than the above
> >method, but I did forget to ask, are their directories in the folder
> >that shouldnt be counted?
>
> >Thats a problem with the above example by thib also, file names without
> >extensions wouldnt be counted.
>
> >A sure fire way of doing it would be:
>
> ><?php
> >$dir = './directory';
> >function countfiles($dir = '.')
> >{
> > $count = 0;
> > $dir = rtrim($dir,'/').'/';
> > if(!is_dir($dir)) return false;
> > foreach(new DirectoryIterator($dir) as $file)
> > {
> > if(is_file($dir.$file)) $count++;
> > }
> > return $count;
> >}
> >echo countfiles();
> >?>
>
> >Of course, this is PHP5, and if you dont have PHP5, you should be
> >changing hosts :P Use these new SPL functions given to us for faster code!
>
> That one worked. Yes, there is a subdirectory in the
> directory in question and no it shouldn't be counted for what my
> purposes are.
>
> Is there any way to limit it to count .jpg files (I don't
> see any screening in this example)? I want to return the number of
> images in the directory (all of them are .jpg and will continue to be)
> and not any text, html, php, or other types of files.
>
> I'm surprised that PHP doesn't have functions built into it to
> do this.
>
> The site I am doing this on is running PHP version 5.2.5.
>
> Regards,
>
> Fred
You may also look into the glob() function and use count() on the
array.
ex:
$count = count(glob("*.jpg")) - 2;
Not sure on the speed implications of it, however.
[Back to original message]
|