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

发布于 2024-08-18 07:07:24 字数 2584 浏览 2 评论 0原文

我试图根据最内层目录名称的名称输出一个类,但无法正确执行。任何帮助将非常感激。它是一个 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”,而下面的后续/后续目录将根据其自己的文件夹名称拥有其类。

I am trying to output a class based on the name of innermost directory name, but can't get it right. Any help would be very much appreciated. It's a php_file_tree, please see section numbered #39 below:

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

The topmost directory will always have a class "php-file-tree", while the subsequent/ following directories underneath will have their classes based on their own folder names.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把时间冻结 2024-08-25 07:07:24

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

抱歉打扰大家了。谢谢

Geez, actually it was okay already. Seems the problem was cache or something as I placed it inside modal dialog with AJAX call. I have tried several possibilities before, including "$first_call = FALSE;" to no avail. But when I revisited the directory again, added "$first_call = FALSE;", and clear everything, it now output the folder name correctly.

Sorry to bother, anyone. Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文