Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File counter

Status
Not open for further replies.

BRKN

Programmer
Joined
Nov 7, 2002
Messages
1
Location
DK
I need a script that will count the number of files in a specified directory. Does such a script exist? Could one of you please tell me the code?
 
Here is a class that you can use. Save it and then use the example at the bottom (this class has many, many uses).

Have fun!

Chad.

dir.class.php:
<?php
/*
dir.class.php
Class for reading a directory structure.

AUTHOR:
Chad Horton
(c)2001, Chad Horton, iPAC Development Services
chorton@inlandpac.com

DESCRIPTION:
aFiles contains multiple aFile entries
aFile: Path => relative path eg. ../xx/yy/
File => filename eg. filename (without extension)
Extension => ext
IsDirectory => true/false
FullName => Path . File . &quot;.&quot; . Extension
FileName => File . &quot;.&quot; . Extension

Notes
Filenames with multiple Extensions: only the last extensions is saved as extensions
eg: aaa.bbb.ccc results in File=aaa.bbb and Extension=ccc
Filenames are stored in the same case as the are stored in the filesystem
sFilter is only applied to files.
*/
if ( !defined( &quot;INCLUCDED_DIR&quot; ) )
{
define( &quot;INCLUCDED_DIR&quot;, TRUE );

Class CDir
{
var $aFiles;

Function CDir()
{
$this->Init();
}

Function Init()
{
unset( $this->aFiles );
$this->aFiles = array();
}

// Read
// sPath path eg. &quot;../xx/yy/&quot; (note the last &quot;/&quot;)
// sInclude regular expression for filtering path- and filenames
// fRecursive true/false: go down the whole structure
// fFiles result set will contain entries which are files
// fDirectory result set will contain entries which are directories
// sRoot Root-Path. Will be appended to the entries.
// sExclude regular expression for filtering path- and filenames
Function Read( $sPath, $sInclude = &quot;&quot;, $fRecursive = false, $fFiles = true, $fDirectories = true, $sRoot = &quot;&quot;, $sExclude = &quot;&quot; )
{
$oHandle = opendir( $sPath );
while ( $sFilename = readdir( $oHandle ) )
{
$fInsert = true;

if ( $sFilename == &quot;.&quot; || $sFilename == &quot;..&quot; )
continue;

$fIsDirectory = is_dir( $sPath . $sFilename );

if ( !$fFiles && !$fIsDirectory )
$fInsert = false;
if ( !$fDirectories && $fIsDirectory )
$fInsert = false;

if ( $fInsert && !$fIsDirectory && ( !empty( $sInclude ) || !empty( $sExclude ) ) )
{
$sFullname = $sRoot;
$sFullname .= $sFilename;

if ( !empty( $sInclude ) )
if ( !ereg( $sInclude, $sFullname ) )
$fInsert = false;

if ( !empty( $sExclude ) )
if ( ereg( $sExclude, $sFullname ) )
$fInsert = false;
}

if ( $fInsert )
{
$i = strrpos( $sFilename, &quot;.&quot; ) + 1;
if ( substr( $sFilename, $i - 1, 1 ) == &quot;.&quot; )
{
$sFile = substr( $sFilename, 0, $i - 1 );
$sExtension = substr( $sFilename, $i );
}
else
{
$sFile = $sFilename;
$sExtension = &quot;&quot;;
}

$aFile = array
(
&quot;Path&quot; => $sRoot,
&quot;File&quot; => $sFile,
&quot;Extension&quot; => $sExtension,
&quot;IsDirectory&quot; => $fIsDirectory
);

//Insert current file into aFiles array
$this->aFiles[] = $aFile;
}

//Recursion?
if ( $fRecursive && $fIsDirectory )
$this->Read( $sPath . $sFilename . &quot;/&quot;, $sInclude, $fRecursive, $fFiles, $fDirectories, $sRoot . $sFilename . &quot;/&quot;, $sExclude );
}

closedir( $oHandle );
}

Function Output()
{
reset( $this->aFiles );
while( list( $sKey, $aFile ) = each( $this->aFiles ) )
$this->OutputFile( $aFile );
}

Function OutputFile( $aFile )
{
printf( &quot;Path: %s<br>\n&quot;, $this->GetPath( $aFile ) );
printf( &quot;File: %s<br>\n&quot;, $this->GetFile( $aFile ) );
printf( &quot;Extension: %s<br>\n&quot;, $this->GetExtension( $aFile ) );
printf( &quot;IsDirectory: %s<br>\n&quot;, $this->GetIsDirectory( $aFile ) ? &quot;true&quot; : &quot;false&quot; );
printf( &quot;IsFile: %s<br>\n&quot;, $this->GetIsFile( $aFile ) ? &quot;true&quot; : &quot;false&quot; );
printf( &quot;FullName: %s<br>\n&quot;, $this->FullName( $aFile ) );
printf( &quot;FileName: %s<br>\n&quot;, $this->FileName( $aFile ) );
printf( &quot;DirectoryName: %s<br>\n&quot;, $this->DirectoryName( $aFile ) );
echo &quot;<hr>\n&quot;;
}

Function GetPath( $aFile )
{
return( $aFile[ &quot;Path&quot; ] );
}

Function GetFile( $aFile )
{
return( $aFile[ &quot;File&quot; ] );
}

Function GetExtension( $aFile )
{
return( $aFile[ &quot;Extension&quot; ] );
}

Function GetIsDirectory( $aFile )
{
return( $aFile[ &quot;IsDirectory&quot; ] );
}

Function GetIsFile( $aFile )
{
return( !$this->GetIsDirectory( $aFile ) );
}

Function FullName( $aFile )
{
return( $this->GetPath( $aFile ) . $this->FileName( $aFile ) );
}

Function FileName( $aFile )
{
$sBuffer = $this->DirectoryName( $aFile );
if ( $this->GetIsDirectory( $aFile ) )
$sBuffer .= &quot;/&quot;;

return( $sBuffer );
}

// DirectoryName returns the same as FileName, but without a ending &quot;/&quot; for Directories.
Function DirectoryName( $aFile )
{
$sBuffer = $this->GetExtension( $aFile );
if ( !empty( $sBuffer ) )
$sBuffer = &quot;.&quot; . $sBuffer;
$sBuffer = $this->GetFile( $aFile ) . $sBuffer;

return( $sBuffer );
}
}

}
// if ( !INCLUCDED_DIR )

?>


example:
<?php
require( &quot;dir.class.php&quot; );

$oDir = new CDir();

//path,regexp,recursive,files,directories,root,exclude
$oDir->Read( &quot;/abs/path/to/directory/&quot;, &quot;&quot;, false, true, false, &quot;&quot;, 0 );

echo count($oDir->aFiles);


?>


;-) ICQ: 54380631
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top