使用 PHP,我怎样才能找到文件夹或其子文件夹(任何深度)中的所有 .php 文件?
问问题
19482 次
4 回答
36
您可以使用RecursiveDirectoryIterator
和RecursiveIteratorIterator
:
$di = new RecursiveDirectoryIterator(__DIR__,RecursiveDirectoryIterator::SKIP_DOTS);
$it = new RecursiveIteratorIterator($di);
foreach($it as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) == "php") {
echo $file, PHP_EOL;
}
}
于 2013-02-24T18:34:42.823 回答
8
只需添加如下内容:
function listFolderFiles($dir){
$ffs = scandir($dir);
$i = 0;
$list = array();
foreach ( $ffs as $ff ){
if ( $ff != '.' && $ff != '..' ){
if ( strlen($ff)>=5 ) {
if ( substr($ff, -4) == '.php' ) {
$list[] = $ff;
//echo dirname($ff) . $ff . "<br/>";
echo $dir.'/'.$ff.'<br/>';
}
}
if( is_dir($dir.'/'.$ff) )
listFolderFiles($dir.'/'.$ff);
}
}
return $list;
}
$files = array();
$files = listFolderFiles(dirname(__FILE__));
于 2013-02-24T18:29:58.750 回答
3
我修改了supajason创建 的代码因为提供的代码没有返回一致的结果:主要是由于使用的命名法。
我还添加了一些功能。
<?php
define('ROOT', str_replace('\\', '/', getcwd()).'/');
///########-------------------------------------------------------------
///########-------------------------------------------------------------
///######## FUNCTION TO LIST ALL FILES AND FOLDERS WITHIN A CERTAIN PATH
///########-------------------------------------------------------------
///########-------------------------------------------------------------
function list_folderfiles(
$dir, // *** TARGET DIRECTORY TO SCAN
$return_flat = true, // *** DEFAULT FLAT ARRAY TO BE RETURNED
$iteration = 0 // *** INTERNAL PARAM TO COUNT THE FUNCTIONS OWN ITERATIONS
){
///######## PREPARE ALL VARIABLES
$dir = rtrim($dir, '/'); // *** REMOVE TRAILING SLASH (* just for being pretty *)
$files_folders = scandir($dir); // *** SCAN FOR ALL FILES AND FOLDERS
$nested_folders = []; // *** THE NESTED FOLDERS BUILD ARRAY
static $total_files = []; // *** THE TOTAL FILES ARRAY
///######## MAKE SURE THAT THE STATIC $fileS ARE WILL BE CLEARED AFTER THE FIRST ITERATION, RESET AS EMPTY ARRAY
if($iteration === 0) $total_$files = [];
///######## RUN THROUGH ALL $fileS AND FOLDERS
foreach($files_folders as $file){
///######### IF THE CURRENT ``file`` IS A DIRECTORY UP
if($file === '.' || $file === '..') continue;
///######### IF IT CONCERNS A $file
if(is_dir($dir.'/'.$file)){
$iteration++; // *** RAISE THE ITERATION
$nested_folders[] = list_folderfiles($dir.'/'.$file, false, $iteration); // *** EXECUTE THE FUNCTION ITSELF
}
///######### IF IT CONCERNS A $file
else{
$total_files[] = $dir.'/'.$file; // *** ADD THE $file TO THE TOTAL $fileS ARRAY
$nested_folders[] = $file; // *** ADD THE $file TO THE NESTED FOLDERS ARRAY
}
}
///########==================================================
///######## IF A FLAT LIST SHOULD BE RETURNED
///########==================================================
if($return_flat) return $total_files;
///######## IF A NESTED LIST SHOULD BE RETURNED
else return $nested_folders;
///########==================================================
}
$files = list_folderfiles(ROOT, true); // *** FLAT ARRAY
///$files = list_folderfiles(ROOT, false); // *** NESTED ARRAY
echo print_r($files, true);
于 2015-09-24T07:56:40.173 回答
0
这与此处的另一个答案类似,但SKIP_DOTS
在不需要时将其删除,并且适用于strict_types
:
<?php
$o_dir = new RecursiveDirectoryIterator('.');
$o_iter = new RecursiveIteratorIterator($o_dir);
foreach ($o_iter as $o_info) {
if ($o_info->getExtension() == 'php') {
echo $o_info->getPathname(), "\n";
}
}
于 2020-08-28T20:53:44.837 回答