Documentation

 

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       

1.2.1.1  $TabFiles : Array       

1.2.1.2  $FileCount : integer       

1.2.1.3  $FileSize : integer       

1.2.1.4  $DirCount : integer       

1.2.1.5  $FileExtList : Array       

1.2.2  Methods       

1.2.2.1  SetFileExtListEnable($b)       

1.2.2.2  SetScanSubDirs($b)       

1.2.2.3  SetScanFiles($b)       

1.2.2.4  SetFullDetails($b)       

1.2.2.5  SetFilterExt($arr)       

1.2.2.6  SetFilterEnable($b)       

1.2.2.7  ScanDir($path, $full=false)       

 Description of Files       

 Class_ScanDir.php       

 Example.php       

 Developer Contact       

 Licence       

 

 

 

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

 

1.2.1.1  $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 :

Parameter

Description

Data type

'filename'

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

String

'dirname'

directory of the file

String

'basename'

filename without extension

String

'extension'

filename extension

String

'size'

Size of the file in byte

Integer

'perms'

Permission of the file

Integer

in octal mode (linux mode)

'datechange'

Date of the last changes of the file

Unix Date

'dateaccess'

Date of the last access to the file

Unix Date

'datemodify'

Date of the last modification of the file

Unix Date

'type'

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

String

 

Remark :

In full details, each file have all the informations.

        In light mode, each file have only the “red” parameters

 

 

1.2.1.2  $FileCount : integer

 

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

 

 

1.2.1.3  $FileSize : integer

 

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

 

1.2.1.4  $DirCount : integer

 

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

It doesn't contain the files count (see FileCount)

 

 

1.2.1.5  $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

 

1.2.2.1  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

 

1.2.2.2  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

 

1.2.2.3  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

 

1.2.2.4  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

 

 

1.2.2.5  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 :

        SetFilterExt(array(php”,”jpg”,”gif”));

 

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

 

1.2.2.6  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

 

 

1.2.2.7  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.

 

<?php

/*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.

$Dir->SetFilterEnable(false);

// 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.

$Dir->SetFileExtListEnable(true);

// enable sub directories scan

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

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

$Dir->SetScanSubDirs(true);

// enable Files Scanning

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

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

$Dir->SetScanFiles(true);

// enable full details

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

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

$Dir->SetFullDetails(true);

// run the Directory scanning

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

// run the scan

$Dir->ScanDir($Path,false);

// 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

print_r($Dir->FileExtList);

// 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.