Command

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
Fred Atkinson [ So, 13 Januar 2008 18:46 ] [ ID #1906641 ]

Re: Command

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:
<?php
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 <?php echo
countfiles('dir/subdir/') or '[warning: unable to open dir]'; ?>

-thibī
thyb0 [ So, 13 Januar 2008 20:29 ] [ ID #1906642 ]

Re: Command

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:
> <?php
> 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 <?php echo
> 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 [at] 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 [at] fanetworks.net
title:System Administrator/Web Developer
url:http://fanetworks.net
version:2.1
end:vcard


--------------080503070803070007040105--
Daniel Ennis [ So, 13 Januar 2008 23:49 ] [ ID #1906643 ]

Re: Command

On Sun, 13 Jan 2008 20:29:20 +0100, thibī <thyb0 [at] coralsnake-team.com>
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:
><?php
>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 <?php echo
>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:

<?php
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
Fred Atkinson [ Mo, 14 Januar 2008 00:06 ] [ ID #1906644 ]

Re: Command

Fred Atkinson wrote:
> On Sun, 13 Jan 2008 20:29:20 +0100, thibī <thyb0 [at] coralsnake-team.com>
> 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:
>> <?php
>> 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 <?php echo
>> 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:
>
> <?php
> 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ī
thyb0 [ Mo, 14 Januar 2008 00:20 ] [ ID #1906645 ]

Re: Command

Fred Atkinson wrote:
> On Sun, 13 Jan 2008 20:29:20 +0100, thibī <thyb0 [at] coralsnake-team.com>
> 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:
>> <?php
>> 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 <?php echo
>> 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:
>
> <?php
> 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:

<?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!
--
Daniel Ennis
faNetworks.net - Quality Web Hosting and Ventrilo Services
System Administrator / Web Developer
PHP Developer for 6 years
daniel [at] fanetworks.net
Daniel Ennis [ Mo, 14 Januar 2008 01:32 ] [ ID #1907440 ]

Re: Command

Daniel Ennis wrote:
> Fred Atkinson wrote:
>> On Sun, 13 Jan 2008 20:29:20 +0100, thibī <thyb0 [at] coralsnake-team.com>
>> 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:
>>> <?php
>>> 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 <?php echo
>>> 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:
>> <?php 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:
>
> <?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!

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

-thibī
thyb0 [ Mo, 14 Januar 2008 01:38 ] [ ID #1907441 ]

Re: Command

On Sun, 13 Jan 2008 19:32:22 -0500, Daniel Ennis
<daniel [at] 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
Fred Atkinson [ Mo, 14 Januar 2008 02:59 ] [ ID #1907442 ]

Re: Command

On Sun, 13 Jan 2008 20:59:03 -0500, Fred Atkinson
<fatkinson [at] mishmash.com> 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:

<?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) && 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
Fred Atkinson [ Mo, 14 Januar 2008 05:40 ] [ ID #1907443 ]

Re: Command

Fred Atkinson wrote:
> On Sun, 13 Jan 2008 20:59:03 -0500, Fred Atkinson
> <fatkinson [at] mishmash.com> 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:
>
> <?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) && 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 [at] fanetworks.net
Daniel Ennis [ Mo, 14 Januar 2008 07:43 ] [ ID #1907444 ]

Re: Command

Daniel Ennis wrote:
> Fred Atkinson wrote:
>> On Sun, 13 Jan 2008 20:59:03 -0500, Fred Atkinson
>> <fatkinson [at] mishmash.com> 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:
>> <?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) && 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 [at] fanetworks.net
Daniel Ennis [ Mo, 14 Januar 2008 08:05 ] [ ID #1907445 ]

Re: Command

On Mon, 14 Jan 2008 01:43:36 -0500, Daniel Ennis
<daniel [at] fanetworks.net> 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
Fred Atkinson [ Mo, 14 Januar 2008 18:30 ] [ ID #1907447 ]

Re: Command

On Jan 13, 7:59 pm, Fred Atkinson <fatkin... [at] mishmash.com> wrote:
> On Sun, 13 Jan 2008 19:32:22 -0500, Daniel Ennis
>
>
>
> <dan... [at] 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.
Aaron Saray [ Di, 15 Januar 2008 04:12 ] [ ID #1908460 ]
PHP » alt.php » Command

Vorheriges Thema: Random BGSOUND script
Nächstes Thema: Help needed, new installation, things don't work anymore.