我正在尝试使用PHP列出一个特定文件夹中的所有文件夹和文件。使用下面的代码,我设法列出一个长列表中的所有文件和文件夹。现在我真正要做的就是列出它们,以便我可以使用HTML / jquery将目录结果显示为文件夹旁边带有+标记,这样它们就可以扩展,但我无法弄清楚如何格式化结果PHP就是为了做到这一点。
include('include/class.dirlist.php');
$resources = "/Inetpub/companyweb/resources";
$dir = getDirectoryListing($resources,"a",1,1,"all",1);
$i = 0; //for illustrative purposes only
foreach ($dir as $item) {
echo "<b><a href='resources/".$dir[$i]."'>".$dir[$i]."</a></b><br>";
$i++;
}
上面的代码将输出如下内容:
FOLDER1
FOLDER1 / FILE1.PDF
FOLDER1 / FILE2.PDF
FOLDER2
FOLDER2 / FILE1.PDF
FOLDER2 / FILE2.PDF
以下是我对以下评论的回复:
@ hek2mgl这正是我想要做的。我想树型视图。
@popnoodles如果我能把它变成“ul li”格式,那就是我相信我可以使用的东西。问题是,我是初学PHPer ...所以在这里寻找一个有效的解决方案。
@BjørneMalmanger以下是所包含课程的内容:
function getDirectoryListing($dirname, $sortorder = "a", $show_subdirs = 1, $show_subdirfiles = 0, $exts = "all", $ext_save = 1) {
// This function will return an array with filenames based on the criteria you can set in the variables
// @sortorder : a for ascending (the standard) or d for descending (you can use the "r" for reverse as well, works the same)
// @show_subdirs : 0 for NO, 1 for YES - meaning it will show the names of subdirectories if there are any
// Logically subdirnames will not be checked for the required extentions
// @show_subdirfiles : 0 for NO, 1 for YES - meaning it will show files from the subdirs
// Files from subdirs will be prefixed with the subdir name and checked for the required extentions.
// @exts can be either a string or an array, if not passed to the function, then the default will be a check for common image files
// If exts is set to "all" then all extentions are allowed
// @ext_save : 1 for YES, 0 for NO - meaning it will filter out system files or not (such as .htaccess)
if (!$exts || empty($exts) || $exts == "") {
$exts = array("jpg", "gif", "jpeg", "png", "doc", "xls", "pdf", "tif");
}
if ($handle = opendir($dirname)) {
$filelist = array();
while (false !== ($file = readdir($handle))) {
// Filter out higher directory references
if ($file != "." && $file != "..") {
// Only look at directories or files, filter out symbolic links
if ( filetype ($dirname."/".$file) != "link") {
// If it's a file, check against valid extentions and add to the list
if ( filetype ($dirname."/".$file) == "file" ) {
if (checkFileExtention($file, $exts, $ext_save)) {
$filelist[] = $file;
}
}
// If it's a directory and either subdirs should be listed or files from subdirs add relevant names to the list
else if ( filetype ($dirname."/".$file) == "dir" && ($show_subdirs == 1 || $show_subdirfiles == 1)) {
if ($show_subdirs == 1) {
$filelist[] = $file;
}
if ($show_subdirfiles == 1) {
$subdirname = $file;
$subdirfilelist = getDirectoryListing($dirname."/".$subdirname."/", $sortorder, $show_subdirs, $show_subdirfiles, $exts, $ext_save);
for ($i = 0 ; $i < count($subdirfilelist) ; $i++) {
$subdirfilelist[$i] = $subdirname."/".$subdirfilelist[$i];
}
$filelist = array_merge($filelist, $subdirfilelist);
}
}
}
}
}
closedir($handle);
// Sort the results
if (count($filelist) > 1) {
natcasesort($filelist);
if ($sortorder == "d" || $sortorder == "r" ) {
$filelist = array_reverse($filelist, TRUE);
}
}
return $filelist;
}
else {
return false;
}
}
function checkFileExtention($filename, $exts, $ext_save = 1) {
$passed = FALSE;
if ($ext_save == 1) {
if (preg_match("/^\./", $filename)) {
return $passed;
}
}
if ($exts == "all") {
$passed = TRUE;
return $passed;
}
if (is_string($exts)) {
if (eregi("\.". $exts ."$", $filename)) {
$passed = TRUE;
return $passed;
}
} else if (is_array($exts)) {
foreach ($exts as $theExt) {
if (eregi("\.". $theExt ."$", $filename)) {
$passed = TRUE;
return $passed;
}
}
}
return $passed;
}
答案 0 :(得分:2)
看看jstree。我想这就是你想要的。 (我是用jstree完成的)。 Jstree是一个javascript库,可以使用控件将HTML列表(<ul>, <ol>
)或json或xml等树结构渲染为带有“+”的可控树,以打开和关闭您希望的文件夹。
当然jstree只是javascript的一部分。您必须将getDirectoryListing()
的输出格式化为jstree支持的格式之一。目前这是
这是一个基本的例子。当finiehd看起来像这样:
您可以使用树打开和关闭节点。
我已将您的目录列表功能更改为为jstree生成JSON的功能。它实际上源于此SO post以使事情快速起作用。谢谢! :)这是php。将其放在网络服务器的一个文件夹中,并将其命名为tree.php
<?php
header('Content-Type: application/json');
echo json_encode(dir_to_jstree_array(__DIR__));
function dir_to_jstree_array($dir, $order = "a", $ext = array()) {
if(empty($ext)) {
$ext = array (
"jpg", "gif", "jpeg", "png", "doc", "xls", "pdf", "tif"
);
}
$listDir = array(
'data' => basename($dir),
'attr' => array (
'rel' => 'folder'
),
'metadata' => array (
'id' => $dir
),
'children' => array()
);
$files = array();
$dirs = array();
if($handler = opendir($dir))
{
while (($sub = readdir($handler)) !== FALSE)
{
if ($sub != "." && $sub != "..")
{
if(is_file($dir."/".$sub))
{
$extension = pathinfo($dir."/".$sub, PATHINFO_EXTENSION);
if(in_array($extension, $ext)) {
$files []= $sub;
}
}elseif(is_dir($dir."/".$sub))
{
$dirs []= $dir."/".$sub;
}
}
}
if($order === "a") {
asort($dirs);
} else {
arsort($dirs);
}
foreach($dirs as $d) {
$listDir['children'][]= dir_to_jstree_array($d);
}
if($order === "a") {
asort($files);
} else {
arsort($files);
}
foreach($files as $file) {
$listDir['children'][]= $file;
}
closedir($handler);
}
return $listDir;
}
这里有基本的HTML和javascript。给它一个正确的名称并将其放在tree.php
旁边。我正在使用jstree的json_data插件和ajax。此外,我正在使用类型插件为文件夹和文件呈现不同类型的图标。 (如果需要,也可以为每种文件类型设置一个自定义图标)。你必须在html文件旁放置一个正确的folder.png和image.png。
<html>
<head>
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="jstree-v.pre1.0/jquery.jstree.js"></script>
<script type="text/javascript">
$(function() {
$('#dirtree').jstree({
plugins : ["json_data", "themes", "types"],
json_data : {
ajax : {
'url' : 'tree.php'
}
},
'types': {
'types' : {
'folder' : {
'icon' : {
'image' : 'folder.png'
}
},
'default' : {
'icon' : {
'image' : 'image.png'
},
}
}
}
});
});
</script>
</head>
<body>
<div id="dirtree"></div>
</body>
</html>
多数民众赞成! :)请注意,您有许多配置选项,样式选项和jstree插件。你当然会扩展我的榜样。
有关安装jstree和文档的信息,请参阅上面的jstree项目页面链接。