如何获取最里面的目录名称

时间:2010-01-21 14:45:23

标签: php directory

我试图根据最里面的目录名称输出一个类,但是无法正确。任何帮助将非常感谢。这是一个php_file_tree,请参阅下面编号为#39的部分:

    function php_file_tree_dir($directory, $return_link, $extensions = array(), $first_call = TRUE) {
  // Get and sort directories/files
  if (function_exists("scandir")) {
    $file = scandir($directory);
  }
  else { 
    $file = php4_scandir($directory);
  }
  natcasesort($file);
  // Make directories first
  $files = $dirs = array();

  foreach ($file as $this_file) {
    if (is_dir("$directory/$this_file")) {
      $dirs[] = $this_file;
    }
    else $files[] = $this_file;
  }

  $file = array_merge($dirs, $files);

  // Filter unwanted extensions
  if (!empty($extensions)) {
    foreach (array_keys($file) as $key) {
      if (!is_dir("$directory/$file[$key]")) {
        $ext = substr($file[$key], strrpos($file[$key], ".") + 1);
        if (!in_array($ext, $extensions))unset($file[$key]);
      }
    }
  }
  // Use 2 instead of 0 to account for . and .. "directories"
  if (count($file) > 2) {
    $php_file_tree = "<ul";
    if ($first_call) {
      $php_file_tree .= " class=\"php-file-tree clearfix\"";
      $first_call = FALSE;
    }

    // #39, Here needs to output a class based on innermost directory name
    /*
    else {
      $php_file_tree .= " class=\"innertree ". htmlspecialchars(basename(rtrim($directory, '/'))) ." clearfix\"";
    }
    */

    $php_file_tree .= ">";
    foreach ($file as $this_file) {
      if ($this_file != "." && $this_file != "..") {
        if (is_dir("$directory/$this_file")) {
          // Directory
          $php_file_tree .= "<li class=\"pft-directory\"><a class=\"folder \" href=\"#\">". htmlspecialchars($this_file) ."</a>";
          $php_file_tree .= php_file_tree_dir("$directory/$this_file", $return_link, $extensions, FALSE);
          $php_file_tree .= "</li>";
        }
        else  {
          //$ext = "ext-". substr($this_file, strrpos($this_file, ".") + 1); // need to compare speed with native
          $ext = "ext-". pathinfo($this_file, PATHINFO_EXTENSION);
          $link = str_replace("[link]", base_path() ."$directory/". urlencode($this_file), $return_link);
          $php_file_tree .= "<li class=\"pft-file ". strtolower($ext) ."\"><a class=\"screenshot\" title=". htmlspecialchars($this_file) ." href=\"$link\">". htmlspecialchars($this_file) ."</a></li>";
        }
      }
    }
    $php_file_tree .= "</ul>";
  }
  return $php_file_tree;
}

最顶层的目录将始终具有“php-file-tree”类,而下面的后续/后续目录将根据自己的文件夹名称创建类。

1 个答案:

答案 0 :(得分:1)

Geez,实际上已经没事了。似乎问题是缓存或其他东西,因为我把它放在带有AJAX调用的模态对话框中。我之前尝试了几种可能性,包括“$ first_call = FALSE;”无济于事。但是当我再次访问该目录时,添加了“$ first_call = FALSE;”,并清除了所有内容,它现在正确输出了文件夹名称。

很抱歉打扰任何人。谢谢