Используя PHP, как я могу найти все .php-файлы в папке или ее подпапках (любой глубины)?
просто добавьте что-то вроде:
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__));
Вы можете использовать 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; } }
Очень поздно. Но я думаю, что это будет полезно для тех, кто приземлился здесь после обыска.
Я изменил код, созданный субаджейсом. Поскольку предоставленный код не возвращал согласованный результат: главным образом из-за используемой номенклатуры.
Я также добавил некоторые функции.
Надеюсь, это полезно!
<?php define('ROOT', str_replace('\\', '/', getcwd()).'/'); ///########------------------------------------------------------------- ///########------------------------------------------------------------- ///######## FUNCTION TO LIST ALL FILES AND FOLDERS WITHIN A CERTAIN PATH ///########------------------------------------------------------------- ///########------------------------------------------------------------- function listFolderFiles( $dir, /// **** TARGET DIRECTORY TO SCAN $ReturnFlat = 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 *) $FilesFolders = scandir($dir); /// **** SCAN FOR ALL FILES AND FOLDERS $NestedFolders = array(); /// **** THE NESTED FOLDERS BUILD ARRAY static $TotalFiles = array(); /// **** THE TOTAL FILES ARRAY ///######## MAKE SURE THAT THE STATIC FILES ARE WILL BE CLEARED AFTER THE FIRST ITERATION if($iteration === 0){ ///######### RESET AS EMPTY ARRAY $TotalFiles = array(); } ///########================================================== ///########================================================== ///######## RUN THROUGH ALL FILES AND FOLDERS ///########================================================== foreach($FilesFolders as $File){ if( /// **** IF NOT ONE DIR UP ($File != '.') && /// **** IF NOT TWO DIRS UP ($File != '..') ){ ///######### IF IT CONCERNS A FILE if(is_dir($dir.'/'.$File) === true){ $iteration++; /// **** RAISE THE ITERATION $NestedFolders[] = listFolderFiles($dir.'/'.$File, false, $iteration); /// **** EXECUTE THE FUNCTION ITSELF } ///######### IF IT CONCERNS A FILE else{ $TotalFiles[] = $dir.'/'.$File; /// **** ADD THE FILE TO THE TOTAL FILES ARRAY $NestedFolders[] = $File; /// **** ADD THE FILE TO THE NESTED FOLDERS ARRAY } } } ///########================================================== ///####### IF A FLAT LIST SHOULD BE RETURNED if($ReturnFlat === true){ ///########================================================== ///######## RETURN ALL FILES AND FOLDERS IN A FLAT ARRAY ///########================================================== return($TotalFiles); ///########================================================== } ///######## IF A NESTED LIST SHOULD BE RETURNED else{ ///########================================================== ///######## RETURN THE FILES AND FOLDERS IN A NESTED ARRAY ///########================================================== return($NestedFolders); ///########================================================== } } $files = listFolderFiles(ROOT, true); /// **** FLAT ARRAY ///$files = listFolderFiles(ROOT, false); /// **** NESTED ARRAY echo print_r($files, true); ?>