0

如何使用 PHP 在目录中显示文件夹的限制?下面的代码显示了所有文件夹,但我只想查看 10 个文件夹。

function folderlist() {
  $startdir = './';
  $ignoredDirectory[] = '.';
  $ignoredDirectory[] = '..';
  if(is_dir($startdir)) {
    if($dh = opendir($startdir)) {
      while(($folder = readdir($dh)) !== false) {
        if(!(array_search($folder, $ignoredDirectory) > -1)) {
          if(filetype($startdir.$folder) == "dir") {
            $mtime = filemtime($startdir.$folder);
            $directorylist[$mtime]['name'] = $folder;
            $directorylist[$mtime]['path'] = $startdir;
          }
        }
      }

      closedir($dh);
    }
  }

  krsort($directorylist, SORT_NUMERIC);
  return $directorylist;
}

$folders = folderlist();
foreach($folders as $folder) {
  $path = $folder['path'];
  $name = $folder['name'];

  echo '<div class="urbangreymenu"><ul><li><a href="'.$path.'index.php?wallpapers='.$name.'" target="_parent">'.$name.'</a></li></ul></div>';
}
4

1 回答 1

2

更改三行:

function folderlist($limit = 10) {

和 ...

while (($folder = readdir($dh)) !== false && $limit) {

和...

$limit--;

一起:

function folderlist($limit = 10) {
    $startdir = './';
    $ignoredDirectory[] = '.'; 
    $ignoredDirectory[] = '..';
    if (is_dir($startdir)) {
        if ($dh = opendir($startdir)) {
            while (($folder = readdir($dh)) !== false && $limit) {
                if (!(array_search($folder,$ignoredDirectory) > -1)) {
                    if (filetype($startdir . $folder) == "dir") {
                        $mtime = filemtime($startdir . $folder);
                        $directorylist[$mtime]['name'] = $folder;
                        $directorylist[$mtime]['path'] = $startdir;
                        $limit--;
                    }
                }
            }
            //Rest of code unchanged...
于 2013-03-10T07:36:46.520 回答