5416339
5416339

Reputation: 417

How do I list all the contents of a directory?

I want to make an index.html file which links all the files in a directory in an ordered list.

For example, when you go here, you find:

# Parent Directory
# <u>lol.html
# "><script>alert(String.fromCharCode(72)-String.fromCharCode(105)).html
# -AAAAAAAAAAAAAAAAAAAATESTING.html
# 0dl.blogspot.com.html
# 1000-suns.html
# 123-greeting.html
# 151.html
# 1^2+2-.-2^2-+-3^2-+2-.-4^2.html
# 2010-IIT-JEE-Solutions-Fiitjee.html
# 2010-IIT-JEE-Solutions.html

What I want to do:

<a href="http://searchr.us/web-search/&lt%3bu&gt%3blol.html" >&lt;u&gt;lol.html</a>
<a href="http://searchr.us/web-search/&quot%3b&gt%3b&lt%3bscript&gt%3balert(String.fromCharCode(72)-String.fromCharCode(105)).html">http://searchr.us/web-search/&quot%3b&gt%3b&lt%3bscript&gt%3balert(String.fromCharCode(72)-String.fromCharCode(105)).html</a>

And so on...

Upvotes: 1

Views: 403

Answers (2)

Gordon
Gordon

Reputation: 316969

How to list files from a directory with PHP has been answered way too often to answer it here again. However, the listing at that links looks a lot like Apache's built-in directory indexes, which you can enable by putting

Options +Indexes

in an .htaccess file in that folder. See the following links

Upvotes: 1

Poelinca Dorin
Poelinca Dorin

Reputation: 9703

<?php
$dir = getcwd();

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo '<a href="'.$_SERVER['REQUEST_URI'].'/'.$file.'">' . $file . '</a><br />';
        }
        closedir($dh);
    }
}
?>

Be carefull the href value of the a tag must be changed this is only an example to get you started .

Upvotes: 1

Related Questions