I need to present a folder selection of the entire hard drive of the user (script runs on the user pc). From that list the user can choose one or more folders. I am using the jquery library dynatree (http://code.google.com/p/dynatree/) for presentation.
My script works like this:
1.List all folders of a specified drive letter or path and parse it into Dynatree-readable format via this recursive function:
Code:
function getDirectory( $path = '.', $level = 0, $id = 0){
$ignore = array( 'cgi-bin', '.', '..' , 'windows', 'user_folders', 'user_profiles');
$dh = @opendir( $path );
$zaehl = 0;
if($level != 0) echo(', children: [ ');
while( false !== ( $file = readdir( $dh ))){
$zaehl++;
if( !in_array( strtolower($file), $ignore ) ){
$spaces = str_repeat( ' ', ( $level * 4 ) );
if( is_dir( "$path/$file" ) ){
echo('{title: "'.$file.'", isFolder: true, key: "'.$path.'/'.$file.'"');
echo("\n");
getDirectory( "$path/$file", ($level+1), $id.'.'.$zaehl);
echo('},');
echo("\n");
}
}
}
if($level != 0)
{
echo(']');
}
closedir( $dh );
}
2.Present the list using Dynatree:
$("#tree3").dynatree({
checkbox: true,
selectMode: 3,
children: treeData,
onSelect: function(select, node) {
var selKeys = $.map(node.tree.getSelectedNodes(), function(node){
return node.data.key;
});
var selRootNodes = node.tree.getSelectedNodes(true);
var selRootKeys = $.map(selRootNodes, function(node){
return node.data.key;
});
$("#echoSelectionRootKeys3").text(selRootKeys.join(", "));
$("#echoSelectionRoots3").text(selRootNodes.join(", "));
},
onDblClick: function(node, event) {
node.toggleSelect();
},
onKeydown: function(node, event) {
if( event.which == 32 ) {
node.toggleSelect();
return false;
}
}
});
I don´t think that process number 2 is the problem, as i basically copied it from the creators tutorial. I think that I´ve made a mistake during process 1 while listing the folders.
Running the getDirectory script with a parameter of a harddrive with few folders it works like a charm. When it comes to (typically) harddrives with lots of nested folders it lasts there several minutes.
Does anybody know a better way of listing the folders or a complete different solution for presenting a server-side folder creation dialogue?
If you are complaining about speed, I would suggest Lazy Loading. If you are loading every folder/file in your directory, it will become very slow.