List server folders and files as tree with Folder View ASP/PHP
Question:
How can I display the files and sub folders on the server in Folder View?
Answer:
Folder View use unordered list for building the tree. You have to construct the unordered list, in order to display the files and sub-folders of particular folder of your server into a tree.
To do that you can use the following code to get the files and sub-folder and build the list:
(It is provided both for ASP and PHP. Chose the appropriate one depending on your server-side language)
* ASP (VBScript)
If you use ASP VBScript for server-side language, you could add the following code to your page:
<%
Sub ListFolder(path)
Dim fs, folder, file, item
Set fs = CreateObject("Scripting.FileSystemObject")
Set folder = fs.GetFolder(path)
'Display the target folder.
Response.Write("<li>" & folder.Name)
Response.Write("<ul>" & vbCrLf)
If (folder.Files.Count > 0)OR(folder.SubFolders.Count >0) Then
'Display a list of sub folders.
for each item in folder.SubFolders
ListFolder(item.Path)
next
'Display a list of files.
For each item in folder.Files
Response.Write("<li>" & item.Name & "</li>" & vbCrLf)
Next
End If
Response.Write("</ul>" & vbCrLf)
Response.Write("</li>" & vbCrLf)
End Sub
%>
To use the code into your Folder View, you just have to replace the automatically generated unordered list with the execution of the ListFolder function.
Upload is the name of the folder which content will be displayed into the tree.
For example:
<script language="JavaScript" type="text/javascript">
<!--
dmxListToTree({
bullets : 'plusminus',
icons : true,
struct : false,
objId : 'FolderView'
});
//-->
</script>
<ul class="dmxtree" id="FolderView">
<% ListFolder(Server.MapPath("Upload")) %>
</ul>
* PHP
If you use PHP for server-side language, you could try the following code:
<?php
function ListFolder($path)
{
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
//Leave only the lastest folder name
$dirname = end(explode("/", $path));
//display the target folder.
echo ("<li>$dirname\n");
echo "<ul>\n";
while (false !== ($file = readdir($dir_handle)))
{
if($file!="." && $file!="..")
{
if (is_dir($path."/".$file))
{
//Display a list of sub folders.
ListFolder($path."/".$file);
}
else
{
//Display a list of files.
echo "<li>$file</li>";
}
}
}
echo "</ul>\n";
echo "</li>\n";
//closing the directory
closedir($dir_handle);
}
?>
To use the code into your Folder View, you have to replace the automatically generated list with the execution of the ListFolder function.
For example:
<script language="JavaScript" type="text/javascript">
<!--
dmxListToTree({
bullets : 'plusminus',
icons : true,
struct : false,
objId : 'FolderView'
});
//-->
</script>
<ul class="dmxtree" id="FolderView">
<?php ListFolder("Upload"); ?>
</ul>
Notes: Have in mind that the above code will display all the content into defined folder (all files and the sub-folders with their content).
DISCLAIMER:
This is extra complimentary content which purpose is to show additional usage that is not part of the product, i.e. it suggests tips for extending the functionality of the product by the users themselves.
It is provided "as is", without warranty of any kind, express or
implied , including but not limited to the warranties of
merchantability, fitness for a particular purpose and nonfringement of
third party rights.
DMXzone does not take responsibility to
support the suggested content to be fully compatible and working as
intended and does not provide support for extended functionality which
is not included in the current release of the extension.
It is highly recommended that only more advanced developers use it.
Comments
Good one
I tried PHP its working well Thanks :)
You must me logged in to write a comment.