Command
am 13.01.2008 18:46:09 von Fred AtkinsonI'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
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 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´
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--
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
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´
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
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´
On Sun, 13 Jan 2008 19:32:22 -0500, Daniel Ennis
>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
On Sun, 13 Jan 2008 20:59:03 -0500, Fred Atkinson
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
Fred Atkinson wrote:
> On Sun, 13 Jan 2008 20:59:03 -0500, Fred Atkinson
>
>
> 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
Daniel Ennis wrote:
> Fred Atkinson wrote:
>> On Sun, 13 Jan 2008 20:59:03 -0500, Fred Atkinson
>>
>>
>> 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
On Mon, 14 Jan 2008 01:43:36 -0500, Daniel Ennis
>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
On Jan 13, 7:59 pm, Fred Atkinson
> On Sun, 13 Jan 2008 19:32:22 -0500, Daniel Ennis
>
>
>
>
> >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.