- glob
- Parameters
- Return Values
- Examples
- Notes
- See Also
- User Contributed Notes 49 notes
- Поиск файлов в PHP
- Поиск в директории
- Список всех файлов и директорий
- Результат:
- Только файлы
- Результат:
- Только директории
- Результат:
- Поиск по расширению
- Результат:
- Поиск по нескольким расширениям
- Результат:
- Поиск по имени файла
- Результат:
- Результат:
- Поиск в дереве
- Список всех файлов
- Результат:
- Список всех директорий
- Результат:
- Поиск по имени/расширению
- Результат:
glob
The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells.
Parameters
The pattern. No tilde expansion or parameter substitution is done.
- * — Matches zero or more characters.
- ? — Matches exactly one character (any character).
- [. ] — Matches one character from a group of characters. If the first character is ! , matches any character not in the group.
- \ — Escapes the following character, except when the GLOB_NOESCAPE flag is used.
- GLOB_MARK — Adds a slash (a backslash on Windows) to each directory returned
- GLOB_NOSORT — Return files as they appear in the directory (no sorting). When this flag is not used, the pathnames are sorted alphabetically
- GLOB_NOCHECK — Return the search pattern if no files matching it were found
- GLOB_NOESCAPE — Backslashes do not quote metacharacters
- GLOB_BRACE — Expands to match ‘a’, ‘b’, or ‘c’
- GLOB_ONLYDIR — Return only directory entries which match the pattern
- GLOB_ERR — Stop on read errors (like unreadable directories), by default errors are ignored.
Note: The GLOB_BRACE flag is not available on some non GNU systems, like Solaris or Alpine Linux.
Return Values
Returns an array containing the matched files/directories, an empty array if no file matched or false on error.
Note:
On some systems it is impossible to distinguish between empty match and an error.
Examples
Example #1 Convenient way how glob() can replace opendir() and friends.
foreach ( glob ( «*.txt» ) as $filename ) echo » $filename size » . filesize ( $filename ) . «\n» ;
>
?>?php
The above example will output something similar to:
funclist.txt size 44686 funcsummary.txt size 267625 quickref.txt size 137820
Notes
Note: This function will not work on remote files as the file to be examined must be accessible via the server’s filesystem.
Note: This function isn’t available on some systems (e.g. old Sun OS).
See Also
- opendir() — Open directory handle
- readdir() — Read entry from directory handle
- closedir() — Close directory handle
- fnmatch() — Match filename against a pattern
User Contributed Notes 49 notes
Since I feel this is rather vague and non-helpful, I thought I’d make a post detailing the mechanics of the glob regex.
glob uses two special symbols that act like sort of a blend between a meta-character and a quantifier. These two characters are the * and ?
The ? matches 1 of any character except a /
The * matches 0 or more of any character except a /
If it helps, think of the * as the pcre equivalent of .* and ? as the pcre equivalent of the dot (.)
Note: * and ? function independently from the previous character. For instance, if you do glob(«a*.php») on the following list of files, all of the files starting with an ‘a’ will be returned, but * itself would match:
a.php // * matches nothing
aa.php // * matches the second ‘a’
ab.php // * matches ‘b’
abc.php // * matches ‘bc’
b.php // * matches nothing, because the starting ‘a’ fails
bc.php // * matches nothing, because the starting ‘a’ fails
bcd.php // * matches nothing, because the starting ‘a’ fails
It does not match just a.php and aa.php as a ‘normal’ regex would, because it matches 0 or more of any character, not the character/class/group before it.
Executing glob(«a?.php») on the same list of files will only return aa.php and ab.php because as mentioned, the ? is the equivalent of pcre’s dot, and is NOT the same as pcre’s ?, which would match 0 or 1 of the previous character.
glob’s regex also supports character classes and negative character classes, using the syntax [] and [^]. It will match any one character inside [] or match any one character that is not in [^].
With the same list above, executing
glob(«[ab]*.php) will return (all of them):
a.php // [ab] matches ‘a’, * matches nothing
aa.php // [ab] matches ‘a’, * matches 2nd ‘a’
ab.php // [ab] matches ‘a’, * matches ‘b’
abc.php // [ab] matches ‘a’, * matches ‘bc’
b.php // [ab] matches ‘b’, * matches nothing
bc.php // [ab] matches ‘b’, * matches ‘c’
bcd.php // [ab] matches ‘b’, * matches ‘cd’
glob(«[ab].php») will return a.php and b.php
glob(«[^a]*.php») will return:
b.php // [^a] matches ‘b’, * matches nothing
bc.php // [^a] matches ‘b’, * matches ‘c’
bcd.php // [^a] matches ‘b’, * matches ‘cd’
glob(«[^ab]*.php») will return nothing because the character class will fail to match on the first character.
You can also use ranges of characters inside the character class by having a starting and ending character with a hyphen in between. For example, [a-z] will match any letter between a and z, 7 will match any (one) number, etc..
glob also supports limited alternation with . You have to specify GLOB_BRACE as the 2nd argument for glob in order for it to work. So for example, if you executed glob(«.php», GLOB_BRACE) on the following list of files:
all 3 of them would return. Note: using alternation with single characters like that is the same thing as just doing glob(«[abc].php»). A more interesting example would be glob(«te.php», GLOB_BRACE) on:
tent.php
text.php
test.php
tense.php
text.php and tense.php would be returned from that glob.
glob’s regex does not offer any kind of quantification of a specified character or character class or alternation. For instance, if you have the following files:
a.php
aa.php
aaa.php
ab.php
abc.php
b.php
bc.php
with pcre regex you can do ~^a+\.php$~ to return
This is not possible with glob. If you are trying to do something like this, you can first narrow it down with glob, and then get exact matches with a full flavored regex engine. For example, if you wanted all of the php files in the previous list that only have one or more ‘a’ in it, you can do this:
$list = glob ( «a*.php» );
foreach ( $list as $l ) <
if ( preg_match ( «~^a+\.php$~» , $file ))
$files [] = $l ;
>
?>
glob also does not support lookbehinds, lookaheads, atomic groupings, capturing, or any of the ‘higher level’ regex functions.
glob does not support ‘shortkey’ meta-characters like \w or \d.
Поиск файлов в PHP
Для поиска файлов на сервере хорошо подходит функция glob(), которая возвращает список файлов по заданной маске, например:
В маске можно использовать следующие специальные символы:
* | Соответствует нулю или большему количеству любых символов. |
? | Один любой символ. |
[. ] | Один символ входящий в группу. |
[. ] | Один символ не входящий в группу. |
Вхождение подстрок, работает с флагом GLOB_BRACE . | |
\ | Экранирует следующий символ, кроме случаев, когда используется флаг GLOB_NOESCAPE . |
GLOB_MARK | Добавляет слеш к каждой возвращаемой директории. |
GLOB_NOSORT | Возвращает файлы в том виде, в котором они содержатся в директории (без сортировки). Если этот флаг не указан, то имена сортируются по алфавиту. |
GLOB_NOCHECK | Возвращает шаблон поиска, если с его помощью не был найден ни один файл. |
GLOB_NOESCAPE | Обратные слеши не экранируют метасимволы. |
GLOB_BRACE | Раскрывает для совпадения с « a », « b » или « c ». |
GLOB_ONLYDIR | Возвращает только директории, совпадающие с шаблоном. |
GLOB_ERR | Останавливается при ошибках чтения (например, директории без права чтения), по умолчанию ошибки игнорируются. |
Возможно использовать несколько флагов:
$files = glob('/tmp/*.jpg', GLOB_NOSORT|GLOB_ERR);
Далее во всех примерах используется папка tmp со следующим содержимым:
Поиск в директории
Список всех файлов и директорий
$dir = __DIR__ . '/tmp'; $files = array(); foreach(glob($dir . '/*') as $file) < $files[] = basename($file); >print_r($files);
Результат:
Array ( [0] => 1.svg [1] => 2.jpg [2] => 22-f.gif [3] => 22.svg [4] => img.png [5] => path [6] => prod.png [7] => style-1.txt [8] => style-2.css )
Только файлы
$dir = __DIR__ . '/tmp'; $files = array(); foreach(glob($dir . '/*') as $file) < if (is_file($file)) < $files[] = basename($file); >> print_r($files);
Результат:
Array ( [0] => 1.svg [1] => 2.jpg [2] => 22-f.gif [3] => 22.svg [4] => img.png [5] => prod.png [6] => style-1.txt [7] => style-2.css )
Только директории
$dir = __DIR__ . '/tmp'; $files = array(); foreach(glob($dir . '/*') as $file) < if (is_dir($file)) < $files[] = basename($file); >> print_r($files);
Результат:
Поиск по расширению
$dir = __DIR__ . '/tmp'; $files = array(); foreach(glob($dir . '/*.svg') as $file) < $files[] = basename($file); >print_r($files);
Результат:
Поиск по нескольким расширениям
$dir = __DIR__ . '/tmp'; $files = array(); foreach(glob($dir . '/*.', GLOB_BRACE) as $file) < $files[] = basename($file); >print_r($files);
Результат:
Array ( [0] => 2.jpg [1] => img.png [2] => prod.png )
Поиск по имени файла
$dir = __DIR__ . '/tmp'; $files = array(); foreach(glob($dir . '/style*.*') as $file) < $files[] = basename($file); >print_r($files);
Результат:
Array ( [0] => style-1.txt [1] => style-2.css )
$dir = __DIR__ . '/tmp'; $files = array(); foreach(glob($dir . '/4*.*', GLOB_BRACE) as $obj) < $files[] = basename($obj); >print_r($files);
Результат:
Array ( [0] => 1.svg [1] => 2.jpg [2] => 22-f.gif [3] => 22.svg )
Поиск в дереве
Список всех файлов
function glob_tree_files($path, $_base_path = null) < if (is_null($_base_path)) < $_base_path = ''; >else < $_base_path .= basename($path) . '/'; >$out = array(); foreach(glob($path . '/*') as $file) < if (is_dir($file)) < $out = array_merge($out, glob_tree_files($file, $_base_path)); >else < $out[] = $_base_path . basename($file); >> return $out; > $dir = __DIR__ . '/tmp'; $files = glob_tree_files($dir); print_r($files);
Результат:
Array ( [0] => 1.svg [1] => 2.jpg [2] => 22-f.gif [3] => 22.svg [4] => img.png [5] => path/icon-rew.png [6] => path/marker.png [7] => path/psd/1.psd [8] => path/psd/2.psd [9] => path/psd/index.psd [10] => path/sh-1.png [11] => path/title-1.png [12] => prod.png [13] => style-1.txt [14] => style-2.css )
Список всех директорий
function glob_tree_dirs($path, $_base_path = null) < if (is_null($_base_path)) < $_base_path = ''; >else < $_base_path .= basename($path) . '/'; >$out = array(); foreach(glob($path . '/*', GLOB_ONLYDIR) as $file) < if (is_dir($file)) < $out[] = $_base_path . basename($file); $out = array_merge($out, glob_tree_dirs($file, $_base_path)); >> return $out; > $dir = __DIR__ . '/tmp'; $files = glob_tree_dirs($dir); print_r($files);
Результат:
Array ( [0] => path [1] => path/psd )
Поиск по имени/расширению
function glob_tree_search($path, $pattern, $_base_path = null) < if (is_null($_base_path)) < $_base_path = ''; >else < $_base_path .= basename($path) . '/'; >$out = array(); foreach(glob($path . '/' . $pattern, GLOB_BRACE) as $file) < $out[] = $_base_path . basename($file); >foreach(glob($path . '/*', GLOB_ONLYDIR) as $file) < $out = array_merge($out, glob_tree_search($file, $pattern, $_base_path)); >return $out; > $path = __DIR__ . '/tmp'; $files = glob_tree_search($path, '*.'); print_r($files);
Результат:
Array ( [0] => 2.jpg [1] => img.png [2] => prod.png [3] => path/icon-rew.png [4] => path/marker.png [5] => path/sh-1.png [6] => path/title-1.png )
Чтобы в результирующих списках выводились полные пути к файлам, достаточно удалить функцию basename() .