PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » File System » File_Find » Manual

Commonly needed functions to search for files and directories

Search methods

Search methods – Types of methods what can be used in search functions

Search methods and their patterns

All search functions use $pattern parameter to specify match for filenames. Format of the $pattern depends on value of another parameter - $pattern_type.

'shell' search mode file masks

File masks are used to select single files and folders or groups of them. Masks may contain common valid file name symbols, wildcards ('*' and '?') and special expressions:

For example, files ftp.exe, fc.exe and f.ext may be selected using mask f*.ex?, mask *co* will select both color.ini and edit.com, mask [c-f,t]*.txt can select config.txt, demo.txt, faq.txt and tips.txt.

You may enter several file masks separated with commas or semicolons. For example, to select all the documents, you can specify *.doc,*.txt,*.wri in search pattern.

You may use exclude masks. An exclude mask is one or multiple file masks that must not be matched by the files matching the mask. The exclude mask is delimited from the main mask by the character '|'.

Usage examples of exclude masks:

  1. *.cpp All files with the extension cpp.
  2. *.*|*.bak,*.tmp All files except for the files with extensions bak and tmp.
  3. *.*| This mask has an error - the character | is entered, but the mask itself is not specified.
  4. *.*|*.bak|*.tmp Also an error - the character | may not be contained in the mask more than once.
  5. |*.bak The same as *|*.bak

The comma (or semicolon) is used for separating file masks from each other, and the '|' character separates include masks from exclude masks.

'shell' match mode is available from version 1.2.0 of File_Find

File_Find::glob()

File_Find::glob() – find matches for a pattern in a directory

Synopsis

require_once 'File/Find.php';

array &File_Find::glob ( string $pattern , string $dirpath , string $pattern_type = 'php' )

Description

Search the directory to find matches for the specified pattern.

Parameter

Return value

array - an array contains all filenames and name of subdirectories, which matches the pattern. Or a PEAR_Error.

Throws

Possible PEAR_Error values
Error code Error message Reason Solution
NULL " Cannot open directory " The given directory could not be opend. Check typing and directory permissions. This can not be caused by a competive processing the archive with Archive_Tar

Note

This function can be called statically.

Example

Find all PHP files in current directory

<?php
include "File/Find.php";

$dir = ".";
$items = &File_Find::glob( '!.*\.php$!', $dir, 'perl' );

print_r($items);
?>

File_Find::maptree()

File_Find::maptree() – create a view map for a directory

Synopsis

require_once 'File/Find.php';

array &File_Find::maptree ( string $directory )

Description

Map the directory tree given by the directory_path parameter.

Parameter

Return value

array - a two element array, the first element containing a list of all the directories, the second element containing a list of all the files.

Note

This function can be called statically.

Example

Get the map of a directory

<?php
include "File/Find.php";

$dir = "File_Find";
list($directories, $files) = File_Find::maptree($dir);

echo "Directories ";
print_r($directories);

echo "Files ";
print_r($files);
?>

The above example will output something similar to:


Directories Array
(
    [0] => File_Find
    [1] => File_Find\dir2
    [2] => File_Find\dir2\2
    [3] => File_Find\dir2\1
    [4] => File_Find\dir2\0
    [5] => File_Find\dir
    [6] => File_Find\dir\txtdir
    [7] => File_Find\dir\dir3
    [8] => File_Find\dir\dir2
)
Files Array
(
    [0] => File_Find\dir2\2\1.txt
    [1] => File_Find\dir2\1\1.txt
    [2] => File_Find\dir2\0\1.txt
    [3] => File_Find\dir\1.txt
    [4] => File_Find\dir\2.txt
    [5] => File_Find\dir\txtdir\5.txt
    [6] => File_Find\dir\dir3\4.bak
    [7] => File_Find\dir\dir3\4.txt
    [8] => File_Find\dir\dir2\3.bak
    [9] => File_Find\dir\dir2\3.txt
)

File_Find::mapTreeMultiple()

File_Find::mapTreeMultiple() – create a recursive view map for a directory

Synopsis

require_once 'File/Find.php';

array &File_Find::mapTreeMultiple ( string $directory , integer $maxrecursion = 0 , integer $count = 0 )

Description

Map the directory tree given by the directory_path parameter. Depending on maxrecursion you get the content of the directory and the content of the subdirectories too.

Parameter

Return value

array - a multidimensional array containing all subdirectories and their files

Note

This function can be called statically.

Example

Get the content of a directory including the content of subdirectories

<?php
include "File/Find.php";

$file = File_Find::mapTreemultiple('/usr/', 1);

print_r($file);
?>

The above example will output something similar to:


Array
(
   [0] => file1.tmp
   [1] => file2.tmp
   ['bin'] => Array
      (
         [0] => readme.txt
      )
)