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开发者_Python百科\"";
}
*/
$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.
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
精彩评论