如何使用php列出一个目录,以便在没有javascript的情况下浏览文件夹?
我正在寻找这个PHP函数:如何使用php列出一个目录,以便在没有javascript的情况下浏览文件夹?,php,treeview,tree,navigation,directory,Php,Treeview,Tree,Navigation,Directory,我正在寻找这个PHP函数: 列出一个目录,递归 在文件夹中导航的功能 没有javascript 通过网址导航,“?p=2 | 1”或类似的内容 按类型然后按名称排序 迪特里 #迪尔特里a{ 文字装饰:无; 颜色:#171717; } #dirTree.a文件{ 颜色:#999999; } #dirTree.active>a{ 字体大小:粗体; } 迪特里 #迪尔特里a{ 文字装饰:无; 颜色:#171717; } #dirTree.a文件{ 颜色:#999999; } #dirTree.a
- 列出一个目录,递归
- 在文件夹中导航的功能
- 没有javascript
- 通过网址导航,“?p=2 | 1”或类似的内容
- 按类型然后按名称排序
迪特里
#迪尔特里a{
文字装饰:无;
颜色:#171717;
}
#dirTree.a文件{
颜色:#999999;
}
#dirTree.active>a{
字体大小:粗体;
}
迪特里
#迪尔特里a{
文字装饰:无;
颜色:#171717;
}
#dirTree.a文件{
颜色:#999999;
}
#dirTree.active>a{
字体大小:粗体;
}
我最近不得不做类似的东西,所以这里有一个函数正好可以做到这一点。或者,您可以将类名添加到文件和文件夹中,以使用CSS设置样式。
所有文件夹都显示为“打开”,尽管使用一点JS,您可以创建一个函数,将一些“打开-关闭”行为添加到列表中
<?php
/**
* function makeDirectoryTree
* iterates recursively through a directory
* and lists it in an unordered list
*
* usage: echo makeDirectoryTree(relative/path/to/directory);
*
* @param string $pathname
* @return string
*/
function makeDirectoryTree($pathname){
$path = realpath($pathname);
if(!is_dir($path)){
return "Path does not exist!";
}
$foldertree = new DOMDocument();
/*
* the rootelement of the tree
*/
$ul[""] = $foldertree->createElement('ul');
$ul[""]->setAttribute('id', 'foldertree_root');
$foldertree->appendChild($ul[""]);
/*
* Files in rootfolder
* if not iterated separately, these files will appear alphabetically between the folders
* instead of on top of the list
*
*/
$iterator = new DirectoryIterator($path);
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile()) {
/*
* the random id could be useful if you want to manipulate an element with JS
* for instance to 'open' or 'close' the folders
* also, add an optional classname to files and folders, so you can
* do some markup with CSS, for instance:
* .folder {color: #f00; list-style-image: url('path/to/images/folder.png');}
* .file {color: #999; list-style-image: url('path/to/images/file.png');}
*/
$random_id = md5(microtime());
$li_element = $foldertree->createElement('li', $fileinfo->getFilename());
$li_element->setAttribute('id', 'li_' . str_ireplace(' ', '', $random_id));
$li_element->setAttribute('class', 'file'); //optional classname
$ul[""]->appendChild($li_element);
}
}
/*
* iterate through the other folders
*/
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name=>$value){
if($value->isDir()){
$relative_path = str_ireplace($path . DIRECTORY_SEPARATOR, "", $value->getPathname());
$path_array = explode(DIRECTORY_SEPARATOR, $relative_path);
$new_dir = array_pop($path_array);
$directory_up = implode(DIRECTORY_SEPARATOR, $path_array);
$random_id = md5(microtime());
$li[$relative_path] = $foldertree->createElement('li', $new_dir);
$li[$relative_path]->setAttribute('id', 'li_' . str_ireplace(' ', '', $random_id));
$li[$relative_path]->setAttribute('class', 'folder'); //optional classname
$ul[$relative_path] = $foldertree->createElement('ul');
$ul[$relative_path]->setAttribute('id', 'ul_' . str_ireplace(' ', '', $random_id));
$li[$relative_path]->appendChild($ul[$relative_path]);
$ul[$directory_up]->appendChild($li[$relative_path]);
$iterator = new DirectoryIterator($value->getPathname());
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile()) {
$random_id = md5(microtime());
$li_element = $foldertree->createElement('li', $fileinfo->getFilename());
$li_element->setAttribute('id', 'li_' . str_ireplace(' ', '', $random_id));
$li_element->setAttribute('class', 'file'); //optional classname
$ul[$relative_path]->appendChild($li_element);
}
}
}
}
return $foldertree->saveHTML();
}
?>
我最近不得不做类似的东西,所以这里有一个函数正好可以做到这一点。或者,您可以将类名添加到文件和文件夹中,以使用CSS设置样式。 所有文件夹都显示为“打开”,尽管使用一点JS,您可以创建一个函数,将一些“打开-关闭”行为添加到列表中
<?php
/**
* function makeDirectoryTree
* iterates recursively through a directory
* and lists it in an unordered list
*
* usage: echo makeDirectoryTree(relative/path/to/directory);
*
* @param string $pathname
* @return string
*/
function makeDirectoryTree($pathname){
$path = realpath($pathname);
if(!is_dir($path)){
return "Path does not exist!";
}
$foldertree = new DOMDocument();
/*
* the rootelement of the tree
*/
$ul[""] = $foldertree->createElement('ul');
$ul[""]->setAttribute('id', 'foldertree_root');
$foldertree->appendChild($ul[""]);
/*
* Files in rootfolder
* if not iterated separately, these files will appear alphabetically between the folders
* instead of on top of the list
*
*/
$iterator = new DirectoryIterator($path);
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile()) {
/*
* the random id could be useful if you want to manipulate an element with JS
* for instance to 'open' or 'close' the folders
* also, add an optional classname to files and folders, so you can
* do some markup with CSS, for instance:
* .folder {color: #f00; list-style-image: url('path/to/images/folder.png');}
* .file {color: #999; list-style-image: url('path/to/images/file.png');}
*/
$random_id = md5(microtime());
$li_element = $foldertree->createElement('li', $fileinfo->getFilename());
$li_element->setAttribute('id', 'li_' . str_ireplace(' ', '', $random_id));
$li_element->setAttribute('class', 'file'); //optional classname
$ul[""]->appendChild($li_element);
}
}
/*
* iterate through the other folders
*/
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name=>$value){
if($value->isDir()){
$relative_path = str_ireplace($path . DIRECTORY_SEPARATOR, "", $value->getPathname());
$path_array = explode(DIRECTORY_SEPARATOR, $relative_path);
$new_dir = array_pop($path_array);
$directory_up = implode(DIRECTORY_SEPARATOR, $path_array);
$random_id = md5(microtime());
$li[$relative_path] = $foldertree->createElement('li', $new_dir);
$li[$relative_path]->setAttribute('id', 'li_' . str_ireplace(' ', '', $random_id));
$li[$relative_path]->setAttribute('class', 'folder'); //optional classname
$ul[$relative_path] = $foldertree->createElement('ul');
$ul[$relative_path]->setAttribute('id', 'ul_' . str_ireplace(' ', '', $random_id));
$li[$relative_path]->appendChild($ul[$relative_path]);
$ul[$directory_up]->appendChild($li[$relative_path]);
$iterator = new DirectoryIterator($value->getPathname());
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile()) {
$random_id = md5(microtime());
$li_element = $foldertree->createElement('li', $fileinfo->getFilename());
$li_element->setAttribute('id', 'li_' . str_ireplace(' ', '', $random_id));
$li_element->setAttribute('class', 'file'); //optional classname
$ul[$relative_path]->appendChild($li_element);
}
}
}
}
return $foldertree->saveHTML();
}
?>
这是一个巨大的问题。通过将其分解为多个组件,您将获得更好的响应。(如何列出文件夹的内容?如何使用锚定标记更改工作目录?等等…)仅仅为了让url分页与目录树列表一起工作将是一个关键。剩下的问题可能更容易解决。这不是一个“让我成为PHP应用程序”网站。这是一个巨大的问题。通过将其分解为多个组件,您将获得更好的响应。(如何列出文件夹的内容?如何使用锚定标记更改工作目录?等等…)仅仅为了让url分页与目录树列表一起工作将是一个关键。剩下的问题可能更容易解决。这不是一个“让我成为PHP应用程序”网站。@Yoshi是的,但我希望家长一直都可见。@Peter看到更新,但仍有很多事情要做。“但我认为作为一个例子已经足够了。”Yoshi Impressing。这么小的代码,这么多的功能。糟糕的是,我自己并不了解一切。但是得到一个功能上符合我要求的代码是一种很好的感觉。经过几天的混乱之后。“RecursiveDirectoryIterator”必须是某个内置的php类?@Peter是的,它附带SPL,看看:@Yoshi如何获得printf(“”,内爆(“/”,$parents),$entry->getBasename())代码>改为回声?希望代码对我来说更易懂一点。@Yoshi是的,但我希望家长在整个过程中都是可见的。@Peter看到了更新,但仍有很多事情要做。“但我认为作为一个例子已经足够了。”Yoshi Impressing。这么小的代码,这么多的功能。糟糕的是,我自己并不了解一切。但是得到一个功能上符合我要求的代码是一种很好的感觉。经过几天的混乱之后。“RecursiveDirectoryIterator”必须是某个内置的php类?@Peter是的,它附带SPL,看看:@Yoshi如何获得printf(“”,内爆(“/”,$parents),$entry->getBasename())代码>改为回声?我希望代码对我来说更容易理解,但问题是我不想使用JS。我不希望所有文件夹都显示为已打开。我在上一篇评论中忘记了你的名字。不知道这是否重要。。。想要som帮助。@Peter没问题,我能帮上什么忙吗?但问题是我不想使用JS。我不希望所有文件夹都显示为已打开。我在上一篇评论中忘记了你的名字。不知道这是否重要。。。想要som帮助。@Peter没问题,我能帮忙吗?