Hi Guys,

I'm looking to build an extremely simple script that would allow someone that I know to share files in a simple manner.
This script should do the following:

  • Make a list of all the files in a specific folder.
  • Make downloadable links to these files (<a href="">).

The person that I'm trying to help with this is blind and wants to share files (papers and theses) with his
students. The less user interaction that is recuired, the better this will work for him. So I figured, why not just
let him copy a folder with all his latest files to the ftp and then let PHP (or HTML) sort out the making of the list.

Could you guys please help me (or us) to a sollution? Hoping something like this already exists.
I've tried many different sources, but these keywords in Google lead to some very unusefull results.

Thanks very much in advance!


Recommended Answers

All 4 Replies

Adapted from the PHP site example:

// path to the directory with files (relative to the script)
$path = '.';
// URL to the above path from the browser
$url = 'http://www.example.com/download/';
if ($handle = opendir($path)) {
    // loop through directory entries
    while (false !== ($entry = readdir($handle))) {
        // skip . and ..
        if ($entry != "." && $entry != "..") {
            // construct a link and display it
            echo "<a href=\"$url$entry\">$entry</a><br>";
Member Avatar

I'd counsel the use of WAI-ARIA with the building of a filetree. I "used to have" an SPL-based tree - I'll see if I can find it.

Thank you so much Broj1, your sollution works really well for me! Nice clickable links and no worries with displaying different types of files, perfect!

I will mark this forum post as 'solved' although I would still like to hear
about the sollution that mr. Diafol has in mind.

Member Avatar

UNfortunately, I "cleaned house" a while back and I think the script went south. Anyway, a close script here:

//FROM http://stackoverflow.com/questions/5417487/sorting-files-per-directory-using-spls-directorytreeiterator
function BuildTree($it, $separator = '--', $level = '') {
    $results = array();
    foreach ($it as $file) {
        if (in_array($file->getBasename(), array('.', '..'))) {
        $tmp = $level . $file->getBaseName();
        if ($it->hasChildren()) {
            $newLevel = $level . $separator;
            $tmp .= "\n" . BuildTree($it->getChildren(), $separator, $newLevel);
        $results[] = $tmp;
    return implode("\n", $results);
$dir = ".";
$it = new RecursiveDirectoryIterator($dir);
$tree = BuildTree($it);
echo "<pre>";
echo "</pre>";

And the WAI-ARIA tree example with javascript control can be seen here...


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.