user15063
user15063

Reputation:

Recursive folder listing in a drop down menu, in php

Im building a file manager which allows users to create folders within folders (within folders) to store their files. I didn't put a real limit on the depth of folders, but I'll probably make it 10 or something like that.

How could I list all the folders (in a tree structure) inside the drop down menu, like so?

/
/Folder 1
/Folder 2
  /Child of folder 2
     /Child of child of folder 2
  /Another Child of of folder 2
/Folder 3

(perhaps a little prettier).

I use mysql to store the folder data

CREATE TABLE IF NOT EXISTS `folders` (
  `f_id` int(11) NOT NULL AUTO_INCREMENT,
  `f_parent` int(11) NOT NULL,
  `f_owner` int(11) NOT NULL,
  `f_name` varchar(255) NOT NULL,
  `f_desc` varchar(1000) NOT NULL,
  `f_added` int(11) NOT NULL,
  `f_files` int(11) NOT NULL,
  `f_private` int(1) NOT NULL,
  `f_password` varchar(255) NOT NULL,
  PRIMARY KEY (`f_id`),
  UNIQUE KEY `f_parent` (`f_parent`,`f_owner`,`f_name`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Upvotes: 2

Views: 1156

Answers (3)

Vaibhav Malushte
Vaibhav Malushte

Reputation: 171

why you are storing folder information in mysql when you can use RecursiveIteratorIterator class

$folder_path="PATH_TO_YOUR_FOLDER" 

$obj= new RecursiveIteratorIterator(new RecursiveDirectoryIterator($folder_path), 
RecursiveIteratorIterator::SELF_FIRST);

foreach($obj as $name => $val){
    echo "$name\n";
}

it will print all the directory and files in the given folder if you want to print only folder name then add this condition

echo $folder (is_dir($name)) ?$name :"";

no need to store folder info in DB,unless you have a specific requirement to store folder information in DB

Upvotes: 1

CodeReaper
CodeReaper

Reputation: 6145

I have found this resource to be a great help when I wanted to build something like it:

http://abeautifulsite.net/blog/2008/03/jquery-file-tree/

The nice thing about that implementation above is that it uses the filesystem to store files and the hierarchy,it won't work for complex stuff, but if your application does not need much it might just the thing you need.

Upvotes: 0

VolkerK
VolkerK

Reputation: 96159

How could I list all the folders (in a tree structure) inside the drop down menu, like so?
What's a "drop down menu"? Tree sounds more like it -> TreeView.

There are many implemenations of a TreeView "control" in Javascript, e.g http://developer.yahoo.com/yui/treeview/, And of course you can use php to feed it with data.

Upvotes: 0

Related Questions