note 83225 added to function.opendir

From: Date: Thu, 15 May 2008 23:22:19 +0000
Subject: note 83225 added to function.opendir
Groups: php.notes 
Request: Send a blank email to php-notes+get-139426@lists.php.net to get a copy of this message
Breadth-first-search (BFS) for a file or directory (vs. Depth-first-search) http://en.wikipedia.org/wiki/Breadth-first_search <?php // Breadth-First Recursive Directory Search, for a File or Directory // with optional black-list paths and optional callback function. // // $root -- is the relative path to the start-of-search directory // $file -- is the qualified file name: 'name.ext' EX: 'data.xml' // (or) $file -- is the target directory name EX: 'xml_files' // $callback -- is an optional function name and will be passed all // matching paths EX: 'my_func', instead of exiting on first match // $omit -- is an optional array of exclude paths -- relative to root // To use $omit but not $callback, pass NULL as the $callback argument // // TESTING VALUES FOLLOW ... function my_func ( $path ) { print "<strong>$path</strong><br>\n"; } $root = '../public_html'; $file = 'data.xml'; $callback = 'my_func'; $omit = array( 'include/img', 'include/css', 'scripts' ); //print breadth_first_file_search ( $root, $file ); //print breadth_first_file_search ( $root, $file, $callback ); //print breadth_first_file_search ( $root, $file, NULL, $omit ); print breadth_first_file_search ( $root, $file, $callback, $omit ); function breadth_first_file_search ( $root, $file, $callback = NULL, $omit = array() ) { $queue = array( rtrim( $root, '/' ).'/' ); // normalize all paths foreach ( $omit as &$path ) { // &$path Req. PHP ver 5.x and later $path = $root.trim( $path, '/' ).'/'; } while ( $base = array_shift( $queue ) ) { $file_path = $base.$file; if ( file_exists( $file_path ) ) { // file found if ( is_callable( $callback ) ) { $callback( $file_path ); // callback => CONTINUE } else { return $file_path; // return file-path => EXIT } } if ( ( $handle = opendir( $base ) ) ) { while ( ( $child = readdir( $handle ) ) !== FALSE ) { if ( is_dir( $base.$child ) && $child != '.' && $child != '..' ) { $combined_path = $base.$child.'/'; if ( !in_array( $combined_path, $omit ) ) { array_push( $queue, $combined_path); } } } closedir( $handle ); } // else unable to open directory => NEXT CHILD } return FALSE; // end of tree, file not found } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 66.190.224.222 ---- Manual Page -- http://www.php.net/manual/en/function.opendir.php Edit -- https://master.php.net/note/edit/83225 Del: integrated -- https://master.php.net/note/delete/83225/integrated Del: useless -- https://master.php.net/note/delete/83225/useless Del: bad code -- https://master.php.net/note/delete/83225/bad+code Del: spam -- https://master.php.net/note/delete/83225/spam Del: non-english -- https://master.php.net/note/delete/83225/non-english Del: in docs -- https://master.php.net/note/delete/83225/in+docs Del: other reasons-- https://master.php.net/note/delete/83225 Reject -- https://master.php.net/note/reject/83225 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#139426) next »