开发者

Listing Files Using PHP

I'm needing to make a web site, that should list all the files that are in the directory /Files(where / is the Apache public - htdocs), but excluding if ther开发者_JAVA百科e is any sub-directory. Also, they should have links for every file. Like this:

echo "<a href='Link to the file'>Test.doc</a>" . "\n"

How could I do it?


glob finds files by a matching pattern. That's not needed here, so you could also use the DirectoryIterator:

foreach(new DirectoryIterator('/pub/files') as $directoryItem) {
    if($directoryItem->isFile()) {
        printf('<a href="/files/%1$s">%1$s</a>', $directoryItem->getBasename());
    }
}

Further reading:

  • Bill Karwin's Putting glob() to the test
  • Introduction to Spl


Use "glob()" function.

<?php
    foreach(glob('/*') as $file)
      if(is_file($file))
        echo '<a href="'.$file.'">'.basename($file).'</a>';
?>

For your info, this kind of thing is called "directory listing", which Apache sometimes does by default when there's no index (html, htm, php asp..) file.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜