感谢大家的投入和建议。
我决定在WP_Theme::scandir()
功能本身。我了解那里发生了什么,没有其他本机WP功能来扫描目录中具有特定扩展名的文件(带有扫描子文件夹的选项)。
P、 我不知道如何链接到WP代码。这是WP_Theme::scandir()
截至2014年1月13日
/**
* Scans a directory for files of a certain extension.
*
* @since 3.4.0
* @access private
*
* @param string $path Absolute path to search.
* @param mixed Array of extensions to find, string of a single extension, or null for all extensions.
* @param int $depth How deep to search for files. Optional, defaults to a flat scan (0 depth). -1 depth is infinite.
* @param string $relative_path The basename of the absolute path. Used to control the returned path
* for the found files, particularly when this function recurses to lower depths.
*/
private static function scandir( $path, $extensions = null, $depth = 0, $relative_path = \'\' ) {
if ( ! is_dir( $path ) )
return false;
if ( $extensions ) {
$extensions = (array) $extensions;
$_extensions = implode( \'|\', $extensions );
}
$relative_path = trailingslashit( $relative_path );
if ( \'/\' == $relative_path )
$relative_path = \'\';
$results = scandir( $path );
$files = array();
foreach ( $results as $result ) {
if ( \'.\' == $result[0] )
continue;
if ( is_dir( $path . \'/\' . $result ) ) {
if ( ! $depth || \'CVS\' == $result )
continue;
$found = self::scandir( $path . \'/\' . $result, $extensions, $depth - 1 , $relative_path . $result );
$files = array_merge_recursive( $files, $found );
} elseif ( ! $extensions || preg_match( \'~\\.(\' . $_extensions . \')$~\', $result ) ) {
$files[ $relative_path . $result ] = $path . \'/\' . $result;
}
}
return $files;
}