function tree($path, $n = 0) { foreach (scandir($path) as $name) { if (canStop($n)) { return; } if ('.' === $name || '..' === $name) { continue; } $f = $path . DIRECTORY_SEPARATOR . $name; if (is_dir($f)) { displayDir($name, $n); tree($f, $n + 1); } else { displayFile($name, $n); } } }
function displayDir($str) { if (!is_dir($str)) { die('不是一个目录!'); } $files = array(); if ($hd = opendir($str)) { while ($file = readdir($hd)) { if ($file != '.' && $file != '..') { if (is_dir($str . '/' . $file)) { $files[$file] = displayDir($str . '/' . $file); } else { $files[] = $file; } } } } return $files; }
function displayDir(SongDir $dir, $displayedDir, $level = 1) { echo '<div id="' . generateId($dir) . '" '; if ($level != 1 && ($displayedDir === null || strncmp($dir->getPath(), $displayedDir->getPath(), strlen($dir->getPath())) != 0)) { echo 'style="display:none;"'; } echo '>'; $subdirs = $dir->getSubdirs(); foreach ($subdirs as $subdir) { for ($i = 0; $i < $level; ++$i) { echo '-- '; } echo "\n"; echo '<a href="#" onClick="show(\'' . generateId($subdir) . '\');return(false)" id="plus">' . "\n"; echo ' <img src="img/folder.png" alt="folder" /> '; echo basename($subdir->getPath()); echo '</a> ' . "\n"; echo '<a href="?play=' . urlencode($subdir->getPath()) . '/' . '">' . "\n"; echo ' <img src="img/play.png" alt="play" />'; echo '</a>'; echo '<br />' . "\n"; displayDir($subdir, $displayedDir, $level + 1); } $songs = $dir->getSongs(); foreach ($songs as $song) { for ($i = 0; $i < $level; ++$i) { echo '-- '; } echo "\n"; echo '<img src="img/music.png" alt="folder" /> ' . "\n"; echo ' ' . $song->getTitle(); echo ' <a href="?play=' . urlencode($song->getPath()) . '">' . "\n"; echo ' <img src="img/play.png" alt="play" />' . "\n"; echo '</a>' . "\n"; echo ' <a href="' . $song->getPath() . '" target="_blank">' . "\n"; echo ' <img src="img/download.png" alt="download" />' . "\n"; echo '</a>' . '<br />' . "\n"; } echo '</div>'; }