hi Im trying to make a script that will recursively search files in a directory and all its sub-directories for a string can any one help?
Here you go friend: <?php $res = array(); $directory = $_SERVER['DOCUMENT_ROOT'].'/path/to/start/searching/'; $arg = opendir($directory); while ($file = readdir($arg)) { if ($file != "." && $file != "..") { if(preg_match('/substring/'), $file)) { $res[] = $file; } } } ?> PHP:
I've modified your code due the fact the TS asked for searching withing subdirectories and content, this altered version will search in subdirectories from given path for given search parameters <?php function startSearching($path, $search, &$return = array()) { $arg = opendir($path); while ($file = readdir($arg)) { if ($file != "." && $file != "..") { if (is_dir($file)) { $return = startSearching($file, $search, $return); } elseif (preg_match('/' . $search . '/'), $file)) { $return[] = $file; } } } return $return; } print_r(startSearching($_SERVER['DOCUMENT_ROOT'].'/path/to/start/searching/', "searchstring"); ?> PHP: Just out of the head, not tested!
I don't do all the work free Eric. People don't learn if you hand them the answers. Then they flood forums with questions because they don't learn.
I know DomainerHelper; but i love to help for free! Error line i think is this one preg_match('/' . $search . '/'), $file) change it to preg_match('/' . $search . '/', $file)