Forum Moderators: coopster

Message Too Old, No Replies

Sub-Directory Drop Down Box after initial selection

         

lander06

3:28 am on Apr 27, 2010 (gmt 0)

10+ Year Member



I finally got my directory listing script to work. I have another problem. After selecting initial directory if that directory has sub-directories, how do can I make a drop down box that is populated with the sub-directory names?

<?php

function readsubdirs($pathname) {
$root = opendir(".");
echo '<select name="'.$pathname.'">'."\n";
$selected = $_POST['seldir'];
while($subs = readdir($root)) {
if(filetype($subs) == "dir") {
if($subs != "." && $subs != "..") {
if($subs == $selected) {
$sel = " selected";
} else {
$sel = "";
}
echo '<option '.$sel.' value="'.$subs.'">'.$subs.'</option>'."\n";
}
}
}
echo '</select>';
}

function getfilelist($root) {
$dir = opendir($root);
$i = 0;
while($file = readdir($dir)) {
if($file != "." && $file != ".." && filetype($file) != "dir") {
if($i%2) {
echo '<div class="filelist_blue"><a href="'.$root.'/'.$file.'">'.$file.'</a></div>'."\n";
} else {
echo '<div class="filelist_gray"><a href="'.$root.'/'.$file.'">'.$file.'</a></div>'."\n";
}
$i++;
}
}
}


?>

<html>
<head>
<style type="text/css">
body {
margin:0;
color:#000000;
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
}

div.top {
top:0;
left:0;
position:relative;
width:100%;
}

div.selectdir {
display:block;
background-color:#FFFFFF;
padding:5px;
}

div.content {
width:100%;
top:10px;
position:relative;
}

div.filelist_blue {
display:block;
background-color:#ffffff;
padding:2px;
padding-left:15px;
}

div.filelist_gray {
display:block;
background-color:#E0E0E0;
padding:2px;
padding-left:15px;
}

div.bottom {
display:block;
color:#999999;
font-size:10px;
padding-top:15px;
text-align:center;
}


</style>

</head>
<body>
</div>
<div class="selectdir">
<form name="seldir" action="#" method="post">

<?
readsubdirs("seldir");
?>

<input type="submit" value="Select Directory" />
</form>
</div>
<div class="content">

<?
if(isset($_POST['seldir'])) {
$root = $_POST['seldir'];
} else {
$root = ".";
}
getfilelist($root);
?>


</div>
</body>
</html>

Matthew1980

10:11 am on Apr 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there lander06,

Right I have tried your code, and it works quite nicely, I am just trying to understand what you want it to do next?

Are you asking for asecondary selection/dropdown to appear under the first so that you can see the contents of the selected dir? Is this so that you allways have the parent directory present?

Instead of eching the select you could build up a var and return that, that IMO makes better use of coding as you are making good use of a functions structure :)

One last thing: try not to use short tags (<?) use the full tags: (<?php) as the short tags are not supported on all servers, and makes for better coding practice :)

Cheers,
MRb

lander06

11:51 am on Apr 27, 2010 (gmt 0)

10+ Year Member



I will change the tags. If I'm going to learn, I have to do it the right way.

To answer your question, I have the select directory. Once selected, if that directory has subdirectories, I would like it to lsit those directories in a separate drop down box below along with the file listing below thatn.

Directory
----SubDir
------SubDir
--------SubDir

No more than levels.

andrewsmd

1:20 pm on Apr 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You may want to look at the is_dir, and glob functions. One question what if you have multiple sub directories i.e.
folder1
..folder1-1
..folder1-2
....folder1-2.1
....folder1-2.2
..folder1-3
....folder1-3.1
....folder1-3.2

Do you need subsequent dropdowns accordingly?

lander06

1:22 pm on Apr 27, 2010 (gmt 0)

10+ Year Member



No. this should only stay about 4 deep

andrewsmd

1:27 pm on Apr 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well my question is, what if it doesn't? It is easy to hard code something that will only go four folders deep, but what if it doesn't always stay that way? To go only four folders deep, you can just use the glob and is_dir functions like I mentioned earlier.

lander06

2:08 pm on Apr 27, 2010 (gmt 0)

10+ Year Member



again Im new and this is my first script..where would this go?

andrewsmd

3:10 pm on Apr 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well here is the potential flaw of only checking four deep. 1. you could have folder structures that are greater than four deep and thus would miss those entries. 2. you could have some sub folders like I mentioned in my previous post. If you just keep going into a sub directory you would only get this result for this folder structure.
Structure
folder1
..folder1-1
..folder1-2
....folder1-2.1
....folder1-2.2
..folder1-3
....folder1-3.1
....folder1-3.2

Result
folder1
..folder1-1
..folder1-2
....folder1-2.1
....folder1-2.2

Because you are just checking forward and not checking the whole directory.
glob returns an array of folders. I.e. if you do a var_dump("C:\\*"); you will see that lists all of the folders and files in your c: directory (note you have to use 2 / because you need to escape the first one). Once you have your folder structure, you can use is_dir which returns whether or not a path is a file or directory. You will need to do this recursively though.
Here is a function that displays an entire folders contents in html. You can edit it to make dropdowns.
ListFolder("C:\\folder\\");

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);
}

andrewsmd

3:51 pm on Apr 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This will list all folders, sub folders, and files. If the folder has contents, it will create a dropdown of the contents under that folder. However, it does not list contents of sub directories. Does this help?


See my last post

[edited by: andrewsmd at 3:54 pm (utc) on Apr 27, 2010]

lander06

3:53 pm on Apr 27, 2010 (gmt 0)

10+ Year Member



Andrew..thank you so much. I'm trying to fit your example into my code and have drop down and I think I'm confusing myself.

trying not to be frustrated but if you can provide more help for the drop downs..that would help. Apologies to you and the rest of the forum.

andrewsmd

3:54 pm on Apr 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No apologies, that's what this forum is for. I had to learn at one time too. In case you missed my last post I edited it so here is the correct code.
ListFolder("C:\\folder\\");

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));

$tempPath = str_ireplace("\\\\", "\\", str_ireplace("/", "\\", $path));

//display the tavrget folder.
echo ("folder: $tempPath<br>");

if(glob($tempPath."\\*")){

echo("<select>");
foreach(glob($tempPath."\\*") as $k){
echo("<option value = '".str_ireplace("\\\\", "\\", $k)."'>".str_ireplace("\\\\", "\\", $k)."</option>");
}//foreach
echo("</select><br><br>");



}//if
else{
echo("No contents<br><br>");
}

//echo the dropdown

// echo("<select>");
// foreach(glob($dirname) as $k){
// echo("<option value = '{$k}'>{$k}</option>");
// }//foreach
// echo("</select><br />");
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.

}
}
}

//closing the directory
closedir($dir_handle);
}

lander06

4:49 pm on Apr 27, 2010 (gmt 0)

10+ Year Member



I think I follow, but I don't see <form> for drop-down(s). Do I key off of $dirname or $k?

This is how my output:

folder: \usr\local\www\data-dist\GoldStandards\Reports\52
No contents

folder: \usr\local\www\data-dist\GoldStandards\Reports\52\SE5100
No contents

andrewsmd

5:03 pm on Apr 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For the form tag, you will need to add that. I just wrote enough to make it view. You will still need to add all of the proper html tags such as doctype head body etc. $dirname is the current directory. $k is when we loop through $directory to output all of the dropdown values. What do you mean by key off of? It depends on what you want.