note 78262 added to function.opendir
| From: | MetaNull at osu1 dot php dot net | Date: | Thu, 04 Oct 2007 19:04:16 +0000 |
| Subject: | note 78262 added to function.opendir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131088@lists.php.net to get a copy of this message | ||
How to find files in a directory, using regular expressions (case-sensitive or not)
<?php
clearstatcache();
$sourcepath = "C:/WINDOWS/TEMP";
// Replace \ by / and remove the final / if any
$root = ereg_replace( "/$", "", ereg_replace( "[\\]", "/",
$sourcepath ));
// Search for text files
$results = m_find_in_dir( $root, ".*\.txt" );
if( false === $results ) {
echo "'{$sourcepath}' is not a valid directory\n";
} else {
print_r( $results );
}
/**
* Search for a file maching a regular expression
*
* @param string $root Root path
* @param string $pattern POSIX regular expression pattern
* @param boolean $recursive Set to true to walk the subdirectories recursively
* @param boolean $case_sensitive Set to true for case sensitive search
* @return array An array of string representing the path of the matching files, or false in case of
error
*/
function m_find_in_dir( $root, $pattern, $recursive = true, $case_sensitive = false ) {
$result = array();
if( $case_sensitive ) {
if( false === m_find_in_dir__( $root, $pattern, $recursive, $result )) {
return false;
}
} else {
if( false === m_find_in_dir_i__( $root, $pattern, $recursive, $result )) {
return false;
}
}
return $result;
}
/**
* @access private
*/
function m_find_in_dir__( $root, $pattern, $recursive, &$result ) {
$dh = @opendir( $root );
if( false === $dh ) {
return false;
}
while( $file = readdir( $dh )) {
if( "." == $file || ".." == $file ){
continue;
}
if( false !== @ereg( $pattern, "{$root}/{$file}" )) {
$result[] = "{$root}/{$file}";
}
if( false !== $recursive && is_dir( "{$root}/{$file}" )) {
m_find_in_dir__( "{$root}/{$file}", $pattern, $recursive, $result );
}
}
closedir( $dh );
return true;
}
/**
* @access private
*/
function m_find_in_dir_i__( $root, $pattern, $recursive, &$result ) {
$dh = @opendir( $root );
if( false === $dh ) {
return false;
}
while( $file = readdir( $dh )) {
if( "." == $file || ".." == $file ){
continue;
}
if( false !== @eregi( $pattern, "{$root}/{$file}" )) {
$result[] = "{$root}/{$file}";
}
if( false !== $recursive && is_dir( "{$root}/{$file}" )) {
m_find_in_dir__( "{$root}/{$file}", $pattern, $recursive, $result );
}
}
closedir( $dh );
return true;
}
?>
----
Server IP: 87.238.162.147
Probable Submitter: 85.201.3.242
----
Manual Page -- http://www.php.net/manual/en/function.opendir.php
Edit -- https://master.php.net/note/edit/78262
Del: integrated -- https://master.php.net/note/delete/78262/integrated
Del: useless -- https://master.php.net/note/delete/78262/useless
Del: bad code -- https://master.php.net/note/delete/78262/bad+code
Del: spam -- https://master.php.net/note/delete/78262/spam
Del: non-english -- https://master.php.net/note/delete/78262/non-english
Del: in docs -- https://master.php.net/note/delete/78262/in+docs
Del: other reasons-- https://master.php.net/note/delete/78262
Reject -- https://master.php.net/note/reject/78262
Search -- https://master.php.net/manage/user-notes.php