Как получить самое внутреннее имя каталога - PullRequest
0 голосов
/ 21 января 2010

Я пытаюсь вывести класс, основанный на имени самого внутреннего имени каталога, но не могу сделать это правильно. Любая помощь будет очень высоко ценится. Это 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 Ответ

1 голос
/ 21 января 2010

Блин, на самом деле все было хорошо. Кажется, проблема в кеше или чем-то еще, поскольку я поместил его в модальное диалоговое окно с вызовом AJAX. Я пробовал несколько вариантов раньше, в том числе "$ first_call = FALSE;" но безрезультатно. Но когда я снова посетил каталог, добавил «$ first_call = FALSE;» и очистил все, теперь он выводит имя папки правильно.

Извините, что беспокою кого угодно. Спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...