Directory tree
Posted: Tue Feb 23, 2010 3:10 am
Hello,
I need hbelp with this. I'd like to get a directory tree in a multidimensional array like:
Here's my funciton. It would be really cool if the searchFiles() would be recursive 
I need hbelp with this. I'd like to get a directory tree in a multidimensional array like:
Code: Select all
<$x = array(
dir1 => array(
0 => file1
1 => file2
2 => array(
dir2 = array(
0 = file3
1 = file4
2 = file5
4 = file6
)
)
)
)Code: Select all
class Default_Model_SongList
{
private $path;
private $files;
private $data;
protected $songs;
public function __construct($path = null)
{
// filter path
$filter = new Zend_Filter_RealPath();
$this->path = $filter->filter($path);
// check if path is valid
if (!is_dir($this->path) || null === $this->path) {
require_once 'Zend/File/Transfer/Exception.php';
throw new Wrll_Exception("That's no path there, matey or, just maybe, you might not have permission!", 201);
}
// get file list
$this->songs = $this->searchFiles($this->path);
// filter the songs
}
public function getSongList()
{
return $this->songs;
}
private function searchFiles($path)
{
if (is_dir($path)) {
$d = dir($path);
while (false !== ($file = $d->read())) {
if ($file != '.' && $file != '..') {
$this->files[$d->path][] = $file;
}
}
foreach ($this->files as $fn) {
foreach ($fn as $n => $file) {
if (is_dir($d->path . '/' . $file)) {
$this->files[$d->path][$n] = ???;
}
}
}
}
return $this->files;
}
}