开发者

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...


<?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 .


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

  • http://www.cyberciti.biz/faq/enabling-apache-file-directory-indexing/
  • http://httpd.apache.org/docs/2.2/mod/core.html#options
  • http://httpd.apache.org/docs/2.2/mod/mod_autoindex.html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜