/    Sign up×
Community /Pin to ProfileBookmark

HELP!!!! PHP script to count files in a folder

Hey guys ive looked all over for this but none of them seem to work with PHP 6!!! so since theres alot of PHP help here i was wondering if anyone know how to do this.

What i want is to have my website homepage have text and then the number of files in the folder like this

Total Accounts Created: ‘Number of files’
Total Characterss Created: ‘Number of files’

What i don’t want though, is for it to read the subdirectory files with the directory files because i need to have the subdirectory display its own number like:

Accounts: 3
Characters: 2

Heres my details that may or may not help
PHP 6
Folders are
C:OldCODBUsers (For the Accounts)
C:OldCODBUsersCharacters (For the characters)

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 02.2010 — [code=php]
/**
* Get number of files in a directory
* @return int
* @param string $directory
* @param bool $includeDirs include directories in count (default is false)
*/
function numFilesInDir($directory, $includeDirs = false)
{
$files = glob($directory.'/*');
if($files === false)
{
user_error(__FUNCTION__."(): Invalid directory ($directory)");
return false;
}
$numFiles = count($files);
if($includeDirs)
{
$dirs = glob($directory.'/*', GLOB_ONLYDIR);
$numFiles = $numFiles - count($dirs);
}
return $numFiles;
}
[/code]
Copy linkTweet thisAlerts:
@SupernovahhauthorApr 02.2010 — That didnt work either ?
Copy linkTweet thisAlerts:
@NogDogApr 02.2010 — That didnt work either ?[/QUOTE]

Define "didn't work". Did you get an error? Can you show me how you called the function?
Copy linkTweet thisAlerts:
@SupernovahhauthorApr 02.2010 — no error at all

not sure if this is correct or not but i copy/pasted and changed my directory

[CODE]<?php
/**
* Get number of files in a directory
* @return int
* @param string $directory
* @param bool $includeDirs include directories in count (default is false)
*/
function numFilesInDir($directory, $includeDirs = false)
{
$files = glob($directory.'C:/OldCODB/Users/*');
if($files === false)
{
user_error(__FUNCTION__."(): Invalid directory ($directory)");
return false;
}
$numFiles = count($files);
if($includeDirs)
{
$dirs = glob($directory.'C:/OldCODB/Users/*', GLOB_ONLYDIR);
$numFiles = $numFiles - count($dirs);
}
return $numFiles;
}
?>[/CODE]
Copy linkTweet thisAlerts:
@SupernovahhauthorApr 02.2010 — Sorry for double post but i tried users/characters for the 2nd one as well
Copy linkTweet thisAlerts:
@NogDogApr 02.2010 — Leave the function definition as I had it, then call the function with the directory:
[code=php]
$numFiles = numFilesInDir('C:/OldCODB/Users');
[/code]
Copy linkTweet thisAlerts:
@SupernovahhauthorApr 02.2010 — so like this?

[CODE]<?php
/**
* Get number of files in a directory
* @return int
* @param string $directory
* @param bool $includeDirs include directories in count (default is false)
*/
$numFiles = numFilesInDir('C:/OldCODB/Users');
function numFilesInDir($directory, $includeDirs = false)
{
$files = glob($directory.'/*');
if($files === false)
{
user_error(__FUNCTION__."(): Invalid directory ($directory)");
return false;
}
$numFiles = count($files);
if($includeDirs)
{
$dirs = glob($directory.'/*', GLOB_ONLYDIR);
$numFiles = $numFiles - count($dirs);
}
return $numFiles;
}
?>[/CODE]


That don't work lemme know if im making any mistakes here (Im not the best php coder in the world xD
Copy linkTweet thisAlerts:
@NogDogApr 02.2010 — Something like this to find out if it worked:
[code=php]
<?php
/**
* Get number of files in a directory
* @return int
* @param string $directory
* @param bool $includeDirs include directories in count (default is false)
*/
function numFilesInDir($directory, $includeDirs = false)
{
$files = glob($directory.'/*');
if($files === false)
{
user_error(__FUNCTION__."(): Invalid directory ($directory)");
return false;
}
$numFiles = count($files);
if($includeDirs)
{
$dirs = glob($directory.'/*', GLOB_ONLYDIR);
$numFiles = $numFiles - count($dirs);
}
return $numFiles;
}
$numFiles = numFilesInDir('C:/OldCODB/Users');
if($numFiles === false)
{
echo "<p>Oops....something went wrong.</p>n";
}
else
{
echo "<p>Found $numFiles files in that directory.</p>n";
}
[/code]
Copy linkTweet thisAlerts:
@SupernovahhauthorApr 03.2010 — YES!!! that worked!! Only thing u needed to change is includedirs to true, for some reason with false it made it count the directories lol
Copy linkTweet thisAlerts:
@NogDogApr 03.2010 — YES!!! that worked!! Only thing u needed to change is includedirs to true, for some reason with false it made it count the directories lol[/QUOTE]

Oops: just negate that condition.
<i>
</i>if([COLOR="Red"][b]![/b][/COLOR]$includeDirs)
×

Success!

Help @Supernovahh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...