Good day.

Tell me how I can find out the file names in the folder of only a certain extension, for example, .php, .txt.
I $files = scandir("application/"); files $files = scandir("application/"); but there are a lot of them here.

It is necessary to show files here only with a certain extension. How to do it?

Thank.

  • @ Roman Rakzin, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina

2 answers 2

Read the comments at https://php.net/manual/ru/function.scandir.php

 <?php function getDirectoryTree( $outerDir , $x){ $dirs = array_diff( scandir( $outerDir ), Array( ".", ".." ) ); $dir_array = Array(); foreach( $dirs as $d ){ if( is_dir($outerDir."/".$d) ){ $dir_array[ $d ] = getDirectoryTree( $outerDir."/".$d , $x); }else{ if (($x)?ereg($x.'$',$d):1) $dir_array[ $d ] = $d; } } return $dir_array; } $dirlist = getDirectoryTree('filmes','flv'); ?> 

    Without subdirectories

     $files = new RegexIterator(new DirectoryIterator($path), '~\.(?:php|txt)~i'); 

    With subdirectories - http://php.net/manual/en/class.recursivedirectoryiterator.php#97228 , there is such magic that I am not ready to sit and understand yet.

    Well, traditionally - why is the whole question of the code made out? Never once did not answer.