| 51 | | Convert('.'); |
| | 52 | |
| | 53 | /* |
| | 54 | * Find files in a directory matching a pattern |
| | 55 | * |
| | 56 | * |
| | 57 | * Paul Gregg <pgregg@pgregg.com> |
| | 58 | * 20 March 2004, Updated 20 April 2004 |
| | 59 | * |
| | 60 | * Open Source Code: If you use this code on your site for public |
| | 61 | * access (i.e. on the Internet) then you must attribute the author and |
| | 62 | * source web site: http://www.pgregg.com/projects/php/code/preg_find.phps |
| | 63 | * Working example: http://www.pgregg.com/projects/php/code/preg_find_ex.phps |
| | 64 | * |
| | 65 | */ |
| | 66 | |
| | 67 | define('PREG_FIND_RECURSIVE', 1); |
| | 68 | define('PREG_FIND_DIRMATCH', 2); |
| | 69 | define('PREG_FIND_FULLPATH', 4); |
| | 70 | define('PREG_FIND_NEGATE', 8); |
| | 71 | define('PREG_FIND_DIRONLY', 16); |
| | 72 | define('PREG_FIND_RETURNASSOC', 32); |
| | 73 | |
| | 74 | // PREG_FIND_RECURSIVE - go into subdirectorys looking for more files |
| | 75 | // PREG_FIND_DIRMATCH - return directorys that match the pattern also |
| | 76 | // PREG_FIND_DIRONLY - return only directorys that match the pattern (no files) |
| | 77 | // PREG_FIND_FULLPATH - search for the pattern in the full path (dir+file) |
| | 78 | // PREG_FIND_NEGATE - return files that don't match the pattern |
| | 79 | // PREG_FIND_RETURNASSOC - Instead of just returning a plain array of matches, |
| | 80 | // return an associative array with file stats |
| | 81 | // to use more than one simply seperate them with a | character |
| | 82 | |
| | 83 | |
| | 84 | |
| | 85 | // Search for files matching $pattern in $start_dir. |
| | 86 | // if args contains PREG_FIND_RECURSIVE then do a recursive search |
| | 87 | // return value is an associative array, the key of which is the path/file |
| | 88 | // and the value is the stat of the file. |
| | 89 | Function preg_find($pattern, $start_dir='.', $args=NULL) { |
| | 90 | |
| | 91 | |
| | 92 | $files_matched = array(); |
| | 93 | |
| | 94 | $fh = opendir($start_dir); |
| | 95 | |
| | 96 | while (($file = readdir($fh)) !== false) { |
| | 97 | if (strcmp($file, '.')==0 || strcmp($file, '..')==0) continue; |
| | 98 | $filepath = $start_dir . '/' . $file; |
| | 99 | if (preg_match($pattern, |
| | 100 | ($args & PREG_FIND_FULLPATH) ? $filepath : $file)) { |
| | 101 | $doadd = is_file($filepath) |
| | 102 | || (is_dir($filepath) && ($args & PREG_FIND_DIRMATCH)) |
| | 103 | || (is_dir($filepath) && ($args & PREG_FIND_DIRONLY)); |
| | 104 | if ($args & PREG_FIND_DIRONLY && $doadd && !is_dir($filepath)) $doadd = false; |
| | 105 | if ($args & PREG_FIND_NEGATE) $doadd = !$doadd; |
| | 106 | if ($doadd) { |
| | 107 | if ($args & PREG_FIND_RETURNASSOC) { // return more than just the filenames |
| | 108 | $fileres = array(); |
| | 109 | if (function_exists('stat')) { |
| | 110 | $fileres['stat'] = stat($filepath); |
| | 111 | $fileres['du'] = $fileres['stat']['blocks'] * 512; |
| | 112 | } |
| | 113 | if (function_exists('fileowner')) $fileres['uid'] = fileowner($filepath); |
| | 114 | if (function_exists('filegroup')) $fileres['gid'] = filegroup($filepath); |
| | 115 | if (function_exists('filetype')) $fileres['filetype'] = filetype($filepath); |
| | 116 | if (function_exists('mime_content_type')) $fileres['mimetype'] = mime_content_type($filepath); |
| | 117 | if (function_exists('dirname')) $fileres['dirname'] = dirname($filepath); |
| | 118 | if (function_exists('basename')) $fileres['basename'] = basename($filepath); |
| | 119 | if (isset($fileres['uid']) && function_exists('posix_getpwuid ')) $fileres['owner'] = posix_getpwuid ($fileres['uid']); |
| | 120 | $files_matched[$filepath] = $fileres; |
| | 121 | } else |
| | 122 | array_push($files_matched, $filepath); |
| | 123 | } |
| | 124 | } |
| | 125 | if ( is_dir($filepath) && ($args & PREG_FIND_RECURSIVE) ) { |
| | 126 | $files_matched = array_merge($files_matched, |
| | 127 | preg_find($pattern, $filepath, $args)); |
| | 128 | } |
| | 129 | } |
| | 130 | |
| | 131 | closedir($fh); |
| | 132 | return $files_matched; |
| | 133 | |
| | 134 | } |