1 | <?php
|
---|
2 | function m_find_in_dir( $root, $pattern, $recursive = true, $case_sensitive = false ) {
|
---|
3 | $result = array();
|
---|
4 | if( $case_sensitive ) {
|
---|
5 | if( false === m_find_in_dir__( $root, $pattern, $recursive, $result )) {
|
---|
6 | return false;
|
---|
7 | }
|
---|
8 | } else {
|
---|
9 | if( false === m_find_in_dir_i__( $root, $pattern, $recursive, $result )) {
|
---|
10 | return false;
|
---|
11 | }
|
---|
12 | }
|
---|
13 |
|
---|
14 | return $result;
|
---|
15 | }
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * @access private
|
---|
19 | */
|
---|
20 | function m_find_in_dir__( $root, $pattern, $recursive, &$result ) {
|
---|
21 | $dh = @opendir( $root );
|
---|
22 | if( false === $dh ) {
|
---|
23 | return false;
|
---|
24 | }
|
---|
25 | while( $file = readdir( $dh )) {
|
---|
26 | if( "." == $file || ".." == $file ){
|
---|
27 | continue;
|
---|
28 | }
|
---|
29 | if( false !== @ereg( $pattern, "{$root}/{$file}" )) {
|
---|
30 | $result[] = "{$root}/{$file}";
|
---|
31 | }
|
---|
32 | if( false !== $recursive && is_dir( "{$root}/{$file}" )) {
|
---|
33 | m_find_in_dir__( "{$root}/{$file}", $pattern, $recursive, $result );
|
---|
34 | }
|
---|
35 | }
|
---|
36 | closedir( $dh );
|
---|
37 | return true;
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * @access private
|
---|
42 | */
|
---|
43 | function m_find_in_dir_i__( $root, $pattern, $recursive, &$result ) {
|
---|
44 | $dh = @opendir( $root );
|
---|
45 | if( false === $dh ) {
|
---|
46 | return false;
|
---|
47 | }
|
---|
48 | while( $file = readdir( $dh )) {
|
---|
49 | if( "." == $file || ".." == $file ){
|
---|
50 | continue;
|
---|
51 | }
|
---|
52 | if( false !== @eregi( $pattern, "{$root}/{$file}" )) {
|
---|
53 | $result[] = "{$root}/{$file}";
|
---|
54 | }
|
---|
55 | if( false !== $recursive && is_dir( "{$root}/{$file}" )) {
|
---|
56 | m_find_in_dir__( "{$root}/{$file}", $pattern, $recursive, $result );
|
---|
57 | }
|
---|
58 | }
|
---|
59 | closedir( $dh );
|
---|
60 | return true;
|
---|
61 | }
|
---|
62 | ?> |
---|