【问题标题】:how to list files from a pathname without listing its subdirectories PHP? [duplicate]如何从路径名列出文件而不列出其子目录 PHP? [复制]
【发布时间】:2015-01-08 14:48:53
【问题描述】:

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

         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); 

【问题讨论】:

  • cmets 见下文。

标签: php


【解决方案1】:

您可以简单地使用 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);
}

【讨论】:

  • 感谢您的意见。我的函数 ListFolderFiles 有效,但似乎问题出在其他地方,即。我的数组函数就像请检查上面代码中的数组部分。数组函数不区分文件夹和文件。
【解决方案2】:

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

function listFolderFiles($dir){
    $items = scandir($dir);

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多