Class ScanDir






- Summary -


 Presentation of the class ScanDir       

 Usage of this class       

 Description of the method and properties of the class       

1.2.1  Properties  $TabFiles : Array  $FileCount : integer  $FileSize : integer  $DirCount : integer  $FileExtList : Array       

1.2.2  Methods  SetFileExtListEnable($b)  SetScanSubDirs($b)  SetScanFiles($b)  SetFullDetails($b)  SetFilterExt($arr)  SetFilterEnable($b)  ScanDir($path, $full=false)       

 Description of Files       



 Developer Contact       





1  Presentation of the class ScanDir


1.1  Usage of this class


The Class ScanDir provide a method to retrieve the list of all files and/or subdirectories from the specified path.

The scan can :
- scan files and/or just the subdirectories
- get only the filename or the full path+filename
- give information of each file (size, dates, permissions, extension, basename) or only filename and size
- the scan can filter the files list on some extensions files only
- during scan, all the unique extensions can be listed in an array
- calculate the total size of files of the scan
- count the number of files and directories

This Class have been developed only because others similar classes seem to be too complex for the usage I need.


Before running the scan, the Class must be setup to enable/disable option.


After the scan complete, you have an associative array with all the files and theirs information.



this class can be use in command line or php website.



1.2  Description of the method and properties of the class


1.2.1  Properties  $TabFiles : Array


This  variable is an array which contains, after scanning directory, the list of files and/or subdirectories


each element of this array is an associative array that contains this informations :



Data type


filename (with full path or not, depending of the setup)



directory of the file



filename without extension



filename extension



Size of the file in byte



Permission of the file


in octal mode (linux mode)


Date of the last changes of the file

Unix Date


Date of the last access to the file

Unix Date


Date of the last modification of the file

Unix Date


type of the file (file always "file", otherwise "dir" for directory)



Remark :

In full details, each file have all the informations.

        In light mode, each file have only the “red” parameters  $FileCount : integer


The parameter FileCount indicates the number of files found, and only files. It doesn't count the directories number. (see DirCount)  $FileSize : integer


During the scan all the size of files are added. At the end , FileSize contains this sum.  $DirCount : integer


Like the FileCount, this property contains the number of subdirectories found.

It doesn't contain the files count (see FileCount)  $FileExtList : Array


This array contains all the extensions files found during the scan. Each extension is unique : If there are   serveral file with same extension, only one extension will be present in this array.

Each element of this array is a string of the extension.




1.2.2  Methods  SetFileExtListEnable($b)


This method enable/disable the listing of all the extensions during Scan.

If $b=True, the option is enable and the extensions are listed

If $b=False, the option is disable. No extension are memorize.

No value is returned  SetScanSubDirs($b)


This method enable/disable the Scanning of subdirectories.

If $b=true, the scan of subdirectories is enable

If $b=false, the scan of subdirectories is disable.

No value is returned  SetScanFiles($b)


This method enable/disable the Scanning of files.

If $b=true, the scan of files is enable

If $b=false, the scan of files is disable.

No value is returned  SetFullDetails($b)


This method enable/disable the record of full details of light informations of files.

If $b=true, the full detail  is enable

If $b=false, the full detail is disable.

No value is returned  SetFilterExt($arr)


This method provide solution to initialise the contain of the extension filter.

The array must contain string, each string is an extension, without the dot before it.

Example :



Remark : This method initialize the filter, but not enable it. You need to use SetFilterEnable to enable/disable the filter use.


No value is returned  SetFilterEnable($b)


This method enable/disable the extension filter process.

If $b=true, the filter is enable

If $b=false, the filter is disable.

No value is returned  ScanDir($path, $full=false)


This is the main method that scan the path put in parameter.

Th $path is a string with the path. The path mustn't have the ending slash or backslash (depending of the OS)

$full enable/disable the full filename (with path in filename) or small filename (without full path).

This method fill the TabFiles property. To get the file list, just check the contain of this array after running the ScanDir method.


No value is returned.

2  Description of Files

2.1  Class_ScanDir.php

The class is provide from only 1 php file, to include in your project. The file Class_ScanDir.php is the class file.


No other dependencies are necessary.


2.2  Example.php


The class is provide with a example php script that explain all the methods and properties usage.

Please refer to this file. (included below)


Remark : Don't forget before using the example to set the $Path variable, with your path to check.



/*Example of use of the Scandir class    */

// include the class ScanDir

include_once "Class_ScanDir.php";

// the path must be an absolute path without the end slash

// ei : $Path="/var/www/test";

$Path = "<your path to directory>";

// instantiate the class.

$Dir = new DirScan () ;

// if needed, set the filter of extension and activate it

// 1 / first define the filter : it will be an array of all extension you want,

// for example here : .php, .jpg, .gif

// in the array string, you only need to put the extension name without dot '.'

$Dir->SetFilterExt(array("php","jpg","gif")) ;

// 2 / just activate it or not. here, the filter is not activate by default.

// you just need to change 'false' by 'true' to enable the filter mode.


// enable the listing off all the extension of files found during scanning

// by specifying true, the scan will keep in an array, all the unique extension

// found during process. this array can be different of the filter array if

// enable, because all the filter extension will not be present in the path found.


// enable sub directories scan

// if "true", the scan process all the subdirectory

// if "false" the scan on scan files in the specified path


// enable Files Scanning

// if "true", the scan process the files

// if "false" the scan do not check files,


// enable full details

// if "false", the only information are filename and size

// if "true", the information are filename, size, dates, perms, type, basename.


// run the Directory scanning

// each new scan will flush the TabFiles properties, to have only the result of the scan

// run the scan


// display some result

echo "<br>Total byte : " .$Dir->FileSize;

echo "<br>Nb Files  : " .$Dir->FileCount;

echo "<br>Nb Dirs  : " .$Dir->DirCount;

echo "<br>List of extension : <br>";

// to see the contains of the list of the extensions files found


// display all the file found during scanning

foreach ($Dir->TabFiles as $f) {

                echo $f["filename"].chr(0xA);





3  Developer Contact


If you want contact, support or if you have remark, please contact mesobius to :


mail : contact AT mesobius dot com


Furthermore, before contacting developer, just check this documentation and example.


4  Licence


The Use of this Class is under the creatives common CC-BY-NC-SA


You can use this class and distribute it in your personal or collaborative project, but not for commercial use.

For commercial use, please contact the developer.


If you apply some modification on the code, please inform the developer. It will put the modification for others.