note 102999 added to function.glob

From: Date: Sat, 19 Mar 2011 08:36:16 +0000
Subject: note 102999 added to function.glob
Groups: php.notes 
Request: Send a blank email to php-notes+get-178000@lists.php.net to get a copy of this message
I thought the safe_glob() function by “BigueNique at yahoo dot ca” was really cool so I incorporated it into a recursive function that will search an entire subdirectory tree. The original function is from http://www.redips.net/php/find-files-with-php/ . Below is the changed version that uses the safe_glob() function. <?php /** * find files matching a pattern * using PHP "glob" function and recursion * * @return array containing all pattern-matched files * * @param string $dir - directory to start with * @param string $pattern - pattern to glob for */ function find_php($dir, $pattern){ // escape any character in a string that might be used to trick // a shell command into executing arbitrary commands $dir = escapeshellcmd($dir); // get a list of all matching files in the current directory $files = safe_glob("$dir/$pattern", $flags = GLOB_PATH); // find a list of all directories in the current directory // directories beginning with a dot are also included foreach (array_merge(safe_glob("$dir/.[^.]*", GLOB_PATH|GLOB_ONLYDIR|GLOB_NODOTS), safe_glob("$dir/*", GLOB_PATH|GLOB_ONLYDIR|GLOB_NODOTS)) as $sub_dir){ $arr = find_php($sub_dir, $pattern); // resursive call $files = array_merge($files, $arr); // merge array with files from subdirectory } // return all found files return $files; } ?> Using this function might look something like this: $file_location = find_php($_SERVER ["DOCUMENT_ROOT"] . "/exaple_directory", $pattern="example_file.jpg"); echo "The file you are looking for is at $file_location[0]"; ---- Server IP: 69.147.83.197 Probable Submitter: 72.173.64.58 ---- Manual Page -- http://www.php.net/manual/en/function.glob.php Edit -- https://master.php.net/note/edit/102999 Del: integrated -- https://master.php.net/note/delete/102999/integrated Del: useless -- https://master.php.net/note/delete/102999/useless Del: bad code -- https://master.php.net/note/delete/102999/bad+code Del: spam -- https://master.php.net/note/delete/102999/spam Del: non-english -- https://master.php.net/note/delete/102999/non-english Del: in docs -- https://master.php.net/note/delete/102999/in+docs Del: other reasons-- https://master.php.net/note/delete/102999 Reject -- https://master.php.net/note/reject/102999 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#178000) next »