note 96490 modified in function.readdir by danbrown
| From: | danbrown@php.net | Date: | Tue, 02 Mar 2010 16:33:10 +0000 |
| Subject: | note 96490 modified in function.readdir by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166535@lists.php.net to get a copy of this message | ||
readdir_recursive() is a recursive readdir() that returns filespecs not filenames.
<?php
define('SEEK', 'the grail');
define('ORIGIN', "C:\\Documents and Settings\\myname\\My Documents");
$hits = array();
$handles = array(ORIGIN=>opendir(ORIGIN)); // Associate handle with its (absolute) path.
////\\\\ "$handles" is special to
// \\\\\\\\\////////
while ($filespec = readdir_recursive($handles)) {
$out = array();
$cmd = "c:\\cygwin\\bin\\strings '$filespec' | c:\\cygwin\\bin\\grep -li
".SEEK;
$lastLine = exec($cmd, $out, $status);
// echo "out is ". implode(' ', $out) .", lastLine is $lastLine, status
is $status\n";
if (strstr(' '. implode(' ', $out), '(standard input)')) { // Hit.
$hits[] = $filespec;
}
}
echo "'".SEEK."' found in files: <br/>\n".
implode("<br/>\n", $hits);
/**
* Return unique filespecs under given directory tree until all found.
*
* To use: while ($filespec = readdir_recursive(array($dir="\\path\\to\\folder" =>
opendir($dir)))) { ...
*
* @param mixed $seed Array(<path>=><resource>) on 1st call | false during recursion.
* @return mixed $filespec String | false
*/
function readdir_recursive($seed = false)
{
global $handles; // ("global" instead of "static" because with latter,
// assignments go out of scope when function executions end (via return $filespec;).)
// $seed is only true (ie, includes a directory path specified by the user)
// when readdir_recursive() is executing for the 1st time.
if ($seed) {
$handles = $seed;
}
assert('is_array($handles)');
if (empty($handles)) {
return false; // All done.
}
// Advance the current($handles) handle to a new file or folder.
$file = readdir(current($handles));
$filespec = key($handles) ."\\". $file;
if (false===$file) { // End of current($handles) directory reached.
// The current element, an exhausted handle, is the last element.
array_pop($handles); // Discard exhausted handle. (This also sets array pointer to array
beginning.)
end($handles);
// echo "\nUp to element ". key($handles) .'=>'. current($handles)
."\n";
return readdir_recursive(); // Resume finding files in a higher level directory.
} elseif (is_file($filespec)) {
return $filespec;
} else { // $file is a directory: Recurse.
if ($file!=='.' && $file!=='..') { // Non-pointer directory
reached.
// Add this subdirectory to the list that must be traversed.
$handles[$filespec] = opendir($filespec);
next($handles); // Prepare to proceed to the appended subdirectory.
}
return readdir_recursive();
}
}
--was--
readdir_recursive() is a recursive readdir() that returns filespecs not filenames.
<?php
define('SEEK', 'the grail');
define('ORIGIN', "C:\\Documents and Settings\\myname\\My Documents");
$hits = array();
$handles = array(ORIGIN=>opendir(ORIGIN)); // Associate handle with its (absolute) path.
////\\\\ "$handles" is special to
// \\\\\\\\\////////
while ($filespec = readdir_recursive($handles)) {
$out = array();
$cmd = "c:\\cygwin\\bin\\strings '$filespec' | c:\\cygwin\\bin\\grep -li
".SEEK;
$lastLine = exec($cmd, $out, $status);
// echo "out is ". implode(' ', $out) .", lastLine is $lastLine, status
is $status\n";
if (strstr(' '. implode(' ', $out), '(standard input)')) { // Hit.
$hits[] = $filespec;
}
}
echo "'".SEEK."' found in files: <br/>\n".
implode("<br/>\n", $hits);
/**
* Return unique filespecs under given directory tree until all found.
*
* To use: while ($filespec = readdir_recursive(array($dir="\\path\\to\\folder" =>
opendir($dir)))) { ...
*
* @param mixed $seed Array(<path>=><resource>) on 1st call | false during recursion.
* @return mixed $filespec String | false
*/
function readdir_recursive($seed = false)
{
global $handles; // ("global" instead of "static" because with latter,
// assignments go out of scope when function executions end (via return $filespec;).)
// $seed is only true (ie, includes a directory path specified by the user)
// when readdir_recursive() is executing for the 1st time.
if ($seed) {
$handles = $seed;
}
assert('is_array($handles)');
if (empty($handles)) {
return false; // All done.
}
// Advance the current($handles) handle to a new file or folder.
$file = readdir(current($handles));
$filespec = key($handles) ."\\". $file;
if (false==$file) { // End of current($handles) directory reached.
// The current element, an exhausted handle, is the last element.
array_pop($handles); // Discard exhausted handle. (This also sets array pointer to array
beginning.)
end($handles);
// echo "\nUp to element ". key($handles) .'=>'. current($handles)
."\n";
return readdir_recursive(); // Resume finding files in a higher level directory.
} elseif (is_file($filespec)) {
return $filespec;
} else { // $file is a directory: Recurse.
if ($file!=='.' && $file!=='..') { // Non-pointer directory
reached.
// Add this subdirectory to the list that must be traversed.
$handles[$filespec] = opendir($filespec);
next($handles); // Prepare to proceed to the appended subdirectory.
}
return readdir_recursive();
}
}
http://php.net/manual/en/function.readdir.php