I frequently need to setup a quick and temporary link for file download either for myself or others, but I hate having to create a new page with links to files that are only going to exist for a few hours. Apache and other web servers often provide this functionality with directory browsing, but I usually want directory browsing disabled or the server that I’m using (that isn’t mine) has directory browsing disabled and I can’t turn it on.

I wrote (and by “wrote”, I mean “wrote some, borrowed some from all over the web–yes, I know I’m not the first to write a script like this) this script to parse a directory and display links for all the files in that folder.

Take the script below and either create a folder called files in the same directory as this file or you can modify the $filesDir variable to point to the location that you’d like to place your temporary downloads. For example, on my server, I have a folder under the root called downloads (http://myserver.com/downloads/) and I place this script in the folder and name it index.php. Within downloads/, I have another folder called files where I place the files for download.

If you want the files in the same folder as the script (that is, you want them all at the same level), I’ve included a check before the links are displayed to make sure that it’s not displaying index.php. If you don’t call this script index.php, you’ll need to modify that piece of the code.

Let me know if you have any questions or comments. As with the usual disclaimer, I’m sure there are enhancements that can be made to this for both performance and security and the code comes as-is and without warranty. :)

<?xml version="1.0" encoding="iso-8858-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
    <title>Download Dropbox</title>
</head>
<body>
    <h1>Download Dropbox</h1>
    <hr />
    <?php
        $filesDir = './files/'; // where you intend to drop files
                                // relative to this page
        $files = array();
        $dir = opendir($filesDir);

        // get the list of files
        while(($file = readdir($dir)) !== false)
        {
            // ignore directories and special files that
            // we don't care about
            // .DS_Store is a Mac system file that I
            // don't want displayed
            if($file !== '.' && $file !== '..' && !is_dir($file) &&
                ($file !== '.DS_Store'))
            {
                $files[] = $file;
            } // end if test
        } // end while loop
        closedir($dir);

        if(count($files) < 1)
        {
            echo "<p>There were no files found.</p>n";
        } // end if test
        else
        {
            // display links for download
            natcasesort($files); // sort files
            echo "<ul>n";

            for($i = 0; $i < count($files); $i++)
            {
                if($files[$i] != "index.php")
                {
                    echo "<li><a href="files/" . $files[$i] . "">" .
                    $files[$i] . "</a></li>n";
                } // end if test
            } // end for loop

            echo "</ul>n";
        } // end else test
    ?>
</body>