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

Php 如何获取最里面的目录名,php,directory,Php,Directory,我试图输出一个基于最内层目录名的类,但无法正确获得它。任何帮助都将不胜感激。这是一个php#u文件树,请参见下面编号为#39的部分: function php_file_tree_dir($directory, $return_link, $extensions = array(), $first_call = TRUE) { // Get and sort directories/files if (function_exists("scandir")) { $file

我试图输出一个基于最内层目录名的类,但无法正确获得它。任何帮助都将不胜感激。这是一个php#u文件树,请参见下面编号为#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\u file\u tree\u dir($directory,$return\u link,$extensions=array(),$first\u call=TRUE){
//获取和排序目录/文件
如果(功能_存在(“扫描”)){
$file=scandir($directory);
}
否则{
$file=php4_scandir($directory);
}
natcasesort($文件);
//首先制作目录
$files=$dirs=array();
foreach($file作为$this\u文件){
if(is_dir(“$directory/$this_file”)){
$dirs[]=$this_文件;
}
else$files[]=$this_文件;
}
$file=array\u merge($dirs,$files);
//过滤不需要的扩展
如果(!空($extensions)){
foreach(数组_键($file)作为$key){
如果(!is_dir(“$directory/$file[$key]”){
$ext=substr($file[$key],strrpos($file[$key],“)+1);
如果(!in_数组($ext,$extensions))未设置($file[$key]);
}
}
}
//使用2而不是0来说明.和..“目录”
如果(计数($file)>2){
$php_file_tree=“”;
foreach($file作为$this\u文件){
如果($this_file!=“&&&$this_file!=”){
if(is_dir(“$directory/$this_file”)){
//目录
$php\u file\u tree.=“
  • ”; $php_file_tree.=php_file_tree_dir($directory/$this_file“,$return_link,$extensions,FALSE); $php_file_tree.=“
  • ”; } 否则{ //$ext=“ext-”.substr($this_file,strrpos($this_file,“.”)+1);//需要将速度与本机速度进行比较 $ext=“ext-”.pathinfo($this_文件,pathinfo_扩展名); $link=str\u replace(“[link]”,base\u path().“$directory/”.urlencode($this\u file),$return\u link); $php\u file\u tree.=“
  • ”; } } } $php_file_tree.=“”; } 返回$php_文件_树; }

    最上面的目录将始终具有一个类“php文件树”,而下面的后续/后续目录将基于其自己的文件夹名称具有其类。

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


    对不起打扰了,各位。谢谢

    那些“bug”是最糟糕的!我感觉到了你的痛苦是的:)我想浏览器仍然保留着旧的页面。希望我做对了。谢谢如果是AJAX调用,缓存的不是浏览器,而是服务器。你可以正常关闭它。