如何列出路径名中的文件而不列出其子目录 PHP


how to list files from a pathname without listing its subdirectories PHP?

使用以下函数,我想列出某个路径(目录)的所有文件,但不列出(考虑到)位于同一路径名下的子目录。

         function listFolderFiles($dir){
            $ffs = scandir($dir);
               echo '<ol>';
         foreach($ffs as $ff){
            if (!is_dir($dir . '/' . $ff)) {
               if( is_file($ff)){
                  listFolderFiles_1($ff);
         }
          echo '</li>';
         }
     }
      echo '</ol>';
   }
   //
   // Array section  
   //Destination data
   $bb = 1;
   $lines_2 = file('C:/Users/TEMP/PHP/Destination_Directory.txt');
   $table = array($lines_2); 

您可以简单地使用 glob() 函数列出文件。

<?php
$dir = "/var/www/";

function listFiles( $dir = '') {
    return $files = glob( $dir . "*.*" ); // Using glob() function. You can also apply filters like *.csv, abc*.txt
}
// Call the function
$files = listFiles( $dir );
// Resulting output of files of the directory
foreach ($files as $file ){
    echo basename($file);
}

使用此代码列出目录中的所有文件。

function listFolderFiles($dir){
    $items = scandir($dir);
    foreach($items as $item){
        if(!is_dir($dir .'/' .$item)){
            echo $item;
            echo '<br/>';
        }
    }
}

请注意,这将不包括子目录下的文件。