Command

Command

am 13.01.2008 18:46:09 von Fred Atkinson

I've looked at php.net and am not able to find this one.

What is the function that will return the number of files in a
subdirectory?

Regards,



Fred

Re: Command

am 13.01.2008 20:29:20 von thyb0

Fred Atkinson wrote:
> I've looked at php.net and am not able to find this one.
>
> What is the function that will return the number of files in a
> subdirectory?
>
> Regards,
>
>
>
> Fred

Just try this one:
function countfiles($dir) {
if( $handle = opendir($dir) ) {
$c = 0;
while( $file = readdir($handle) )
if( !preg_match('/\.\.?/', $file) ) $c++;
return $c;
} else return false;
}
?>

This should make it; you can of course change de regexp filter or use
different methods (this one only avoids counting '.' and '..')

Call example:

Number of files in subdir 'dir/subdir/' is countfiles('dir/subdir/') or '[warning: unable to open dir]'; ?>

-thib´

Re: Command

am 13.01.2008 23:49:41 von Daniel Ennis

This is a multi-part message in MIME format.
--------------080503070803070007040105
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit

thib´ wrote:
> Fred Atkinson wrote:
>> I've looked at php.net and am not able to find this one.
>> What is the function that will return the number of files in a
>> subdirectory?
>> Regards,
>>
>>
>> Fred
>
> Just try this one:
> > function countfiles($dir) {
> if( $handle = opendir($dir) ) {
> $c = 0;
> while( $file = readdir($handle) )
> if( !preg_match('/\.\.?/', $file) ) $c++;
> return $c;
> } else return false;
> }
> ?>
>
> This should make it; you can of course change de regexp filter or use
> different methods (this one only avoids counting '.' and '..')
>
> Call example:
>
> Number of files in subdir 'dir/subdir/' is > countfiles('dir/subdir/') or '[warning: unable to open dir]'; ?>
>
> -thib´

$total = count(new DirectoryIterator('./directoryname'))-2;

should also work too.
-2 because of . and ..

--
Daniel Ennis
faNetworks.net - Quality Web Hosting and Ventrilo Services
System Administrator / Web Developer
PHP Developer for 6 years
daniel@fanetworks.net

--------------080503070803070007040105
Content-Type: text/x-vcard; charset=utf-8;
name="daniel.vcf"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="daniel.vcf"

begin:vcard
fn:Daniel Ennis
n:Ennis;Daniel
org:faNetworks
adr:;;;Fuquay Varina;NC;27526;United States
email;internet:daniel@fanetworks.net
title:System Administrator/Web Developer
url:http://fanetworks.net
version:2.1
end:vcard


--------------080503070803070007040105--

Re: Command

am 14.01.2008 00:06:13 von Fred Atkinson

On Sun, 13 Jan 2008 20:29:20 +0100, thib´
wrote:

>Fred Atkinson wrote:
>> I've looked at php.net and am not able to find this one.
>>
>> What is the function that will return the number of files in a
>> subdirectory?
>>
>> Regards,
>>
>>
>>
>> Fred
>
>Just try this one:
> >function countfiles($dir) {
> if( $handle = opendir($dir) ) {
> $c = 0;
> while( $file = readdir($handle) )
> if( !preg_match('/\.\.?/', $file) ) $c++;
> return $c;
> } else return false;
>}
>?>
>
>This should make it; you can of course change de regexp filter or use
>different methods (this one only avoids counting '.' and '..')
>
>Call example:
>
>Number of files in subdir 'dir/subdir/' is >countfiles('dir/subdir/') or '[warning: unable to open dir]'; ?>
>
>-thib´

I've tried this script. No matter how many files are in the
directory, it says that there is only one file. The directory I used
should have returned thirteen as a value.

I tried making the path '.' so it would count the files in the
same directory it was in. But it again said it there was only one
when there was also thirteen files in that directory.

Here is how I coded the call:

echo countfiles('.') or '[warning: unable to open dir]';
?>

I copied it from your post. The only alterations I made were
to make it three lines and change the directory location.

Regards,



Fred

Re: Command

am 14.01.2008 00:20:17 von thyb0

Fred Atkinson wrote:
> On Sun, 13 Jan 2008 20:29:20 +0100, thib´
> wrote:
>
>> Fred Atkinson wrote:
>>> I've looked at php.net and am not able to find this one.
>>>
>>> What is the function that will return the number of files in a
>>> subdirectory?
>>>
>>> Regards,
>>>
>>>
>>>
>>> Fred
>> Just try this one:
>> >> function countfiles($dir) {
>> if( $handle = opendir($dir) ) {
>> $c = 0;
>> while( $file = readdir($handle) )
>> if( !preg_match('/\.\.?/', $file) ) $c++;
>> return $c;
>> } else return false;
>> }
>> ?>
>>
>> This should make it; you can of course change de regexp filter or use
>> different methods (this one only avoids counting '.' and '..')
>>
>> Call example:
>>
>> Number of files in subdir 'dir/subdir/' is >> countfiles('dir/subdir/') or '[warning: unable to open dir]'; ?>
>>
>> -thib´
>
> I've tried this script. No matter how many files are in the
> directory, it says that there is only one file. The directory I used
> should have returned thirteen as a value.
>
> I tried making the path '.' so it would count the files in the
> same directory it was in. But it again said it there was only one
> when there was also thirteen files in that directory.
>
> Here is how I coded the call:
>
> > echo countfiles('.') or '[warning: unable to open dir]';
> ?>
>
> I copied it from your post. The only alterations I made were
> to make it three lines and change the directory location.
>
> Regards,
>
>
>
> Fred

Sorry, obviously my bad; I made a stupid and classic mistake in the filter;
I successfuly tested it with this new regexp: /^\.\.?$/

You might want to take a look at http://www.regular-expressions.info/ in
order to understand what you're doing ;).

-thib´

Re: Command

am 14.01.2008 01:32:22 von Daniel Ennis

Fred Atkinson wrote:
> On Sun, 13 Jan 2008 20:29:20 +0100, thib´
> wrote:
>
>> Fred Atkinson wrote:
>>> I've looked at php.net and am not able to find this one.
>>>
>>> What is the function that will return the number of files in a
>>> subdirectory?
>>>
>>> Regards,
>>>
>>>
>>>
>>> Fred
>> Just try this one:
>> >> function countfiles($dir) {
>> if( $handle = opendir($dir) ) {
>> $c = 0;
>> while( $file = readdir($handle) )
>> if( !preg_match('/\.\.?/', $file) ) $c++;
>> return $c;
>> } else return false;
>> }
>> ?>
>>
>> This should make it; you can of course change de regexp filter or use
>> different methods (this one only avoids counting '.' and '..')
>>
>> Call example:
>>
>> Number of files in subdir 'dir/subdir/' is >> countfiles('dir/subdir/') or '[warning: unable to open dir]'; ?>
>>
>> -thib´
>
> I've tried this script. No matter how many files are in the
> directory, it says that there is only one file. The directory I used
> should have returned thirteen as a value.
>
> I tried making the path '.' so it would count the files in the
> same directory it was in. But it again said it there was only one
> when there was also thirteen files in that directory.
>
> Here is how I coded the call:
>
> > echo countfiles('.') or '[warning: unable to open dir]';
> ?>
>
> I copied it from your post. The only alterations I made were
> to make it three lines and change the directory location.
>
> Regards,
>
>
>
> Fred

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:

$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!
--
Daniel Ennis
faNetworks.net - Quality Web Hosting and Ventrilo Services
System Administrator / Web Developer
PHP Developer for 6 years
daniel@fanetworks.net

Re: Command

am 14.01.2008 01:38:22 von thyb0

Daniel Ennis wrote:
> Fred Atkinson wrote:
>> On Sun, 13 Jan 2008 20:29:20 +0100, thib´
>> wrote:
>>
>>> Fred Atkinson wrote:
>>>> I've looked at php.net and am not able to find this one.
>>>> What is the function that will return the number of files in a
>>>> subdirectory?
>>>> Regards,
>>>>
>>>>
>>>> Fred
>>> Just try this one:
>>> >>> function countfiles($dir) {
>>> if( $handle = opendir($dir) ) {
>>> $c = 0;
>>> while( $file = readdir($handle) )
>>> if( !preg_match('/\.\.?/', $file) ) $c++;
>>> return $c;
>>> } else return false;
>>> }
>>> ?>
>>>
>>> This should make it; you can of course change de regexp filter or use
>>> different methods (this one only avoids counting '.' and '..')
>>>
>>> Call example:
>>>
>>> Number of files in subdir 'dir/subdir/' is >>> countfiles('dir/subdir/') or '[warning: unable to open dir]'; ?>
>>>
>>> -thib´
>>
>> I've tried this script. No matter how many files are in the
>> directory, it says that there is only one file. The directory I used
>> should have returned thirteen as a value.
>> I tried making the path '.' so it would count the files in the
>> same directory it was in. But it again said it there was only one
>> when there was also thirteen files in that directory.
>> Here is how I coded the call:
>>
>>
>> I copied it from your post. The only alterations I made were
>> to make it three lines and change the directory location.
>> Regards,
>>
>>
>> Fred
>
> 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:
>
> > $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!

Good to know, is that really that faster?
Well, FM', where are you. Ah', there..

-thib´

Re: Command

am 14.01.2008 02:59:03 von Fred Atkinson

On Sun, 13 Jan 2008 19:32:22 -0500, Daniel Ennis
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:
>
> >$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

Re: Command

am 14.01.2008 05:40:21 von Fred Atkinson

On Sun, 13 Jan 2008 20:59:03 -0500, Fred Atkinson
wrote:

OK,

I think I've got it.

I slightly rewrote your script (one line with an if statement)
to include a second comparison.

Here it is:

$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) && preg_match("/.jpg\z/i",
$file))) $count++;
}
return $count;
}
echo countfiles();
echo " file(s) in this directory."
?>

It seems to work just fine. It ignores anything except .jpg
files.

Regards,



Fred

Re: Command

am 14.01.2008 07:43:36 von Daniel Ennis

Fred Atkinson wrote:
> On Sun, 13 Jan 2008 20:59:03 -0500, Fred Atkinson
> wrote:
>
> OK,
>
> I think I've got it.
>
> I slightly rewrote your script (one line with an if statement)
> to include a second comparison.
>
> Here it is:
>
> > $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) && preg_match("/.jpg\z/i",
> $file))) $count++;
> }
> return $count;
> }
> echo countfiles();
> echo " file(s) in this directory."
> ?>
>
> It seems to work just fine. It ignores anything except .jpg
> files.
>
> Regards,
>
>
>
> Fred

GJ :) Hope what ever your working on comes out well!

And yes, the default PHP5 functions will be faster than old style code,
as its optimized C++ doing all the tasks you would normally do manually
in PHP4.

--
Daniel Ennis
faNetworks.net - Quality Web Hosting and Ventrilo Services
System Administrator / Web Developer
PHP Developer for 6 years
daniel@fanetworks.net

Re: Command

am 14.01.2008 08:05:35 von Daniel Ennis

Daniel Ennis wrote:
> Fred Atkinson wrote:
>> On Sun, 13 Jan 2008 20:59:03 -0500, Fred Atkinson
>> wrote:
>>
>> OK,
>> I think I've got it.
>> I slightly rewrote your script (one line with an if statement)
>> to include a second comparison.
>> Here it is:
>> >> $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) && preg_match("/.jpg\z/i",
>> $file))) $count++;
>> }
>> return $count;
>> }
>> echo countfiles();
>> echo " file(s) in this directory."
>> ?>
>>
>> It seems to work just fine. It ignores anything except .jpg
>> files.
>> Regards,
>>
>>
>> Fred
>
> GJ :) Hope what ever your working on comes out well!
>
> And yes, the default PHP5 functions will be faster than old style code,
> as its optimized C++ doing all the tasks you would normally do manually
> in PHP4.
>

And using
if((is_file($dir.$file) && strrchr($fileName, '.') == '.jpg') $count++;


would likely be faster than using regex. Such a simple check doesnt
really need the full power of regex.

--
Daniel Ennis
faNetworks.net - Quality Web Hosting and Ventrilo Services
System Administrator / Web Developer
PHP Developer for 6 years
daniel@fanetworks.net

Re: Command

am 14.01.2008 18:30:54 von Fred Atkinson

On Mon, 14 Jan 2008 01:43:36 -0500, Daniel Ennis
wrote:

>GJ :) Hope what ever your working on comes out well!
>
>And yes, the default PHP5 functions will be faster than old style code,
>as its optimized C++ doing all the tasks you would normally do manually
>in PHP4.

Thanks, Daniel,

I really appreciate the help.

I'm still surprised the PHP doesn't have a built-in function
to do that. I was just looking for the keyword for such a function.
But this will do the job.

Regards,




Fred

Re: Command

am 15.01.2008 04:12:29 von Aaron Saray

On Jan 13, 7:59 pm, Fred Atkinson wrote:
> On Sun, 13 Jan 2008 19:32:22 -0500, Daniel Ennis
>
>
>
> 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:
>
> > > >$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.