[英]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 there is any sub-directory. 我需要创建一个网站,该网站应该列出目录
/Files
(其中/
是Apache公共目录htdocs
)中的所有文件,但不包括是否存在任何子目录。 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. glob
通过匹配的模式查找文件。 That's not needed here, so you could also use the DirectoryIterator
: 这里不需要这样做,因此您也可以使用
DirectoryIterator
:
foreach(new DirectoryIterator('/pub/files') as $directoryItem) {
if($directoryItem->isFile()) {
printf('<a href="/files/%1$s">%1$s</a>', $directoryItem->getBasename());
}
}
Further reading: 进一步阅读:
Use "glob()" function. 使用“ glob()”函数。
<?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. 对于您的信息,这种事情称为“目录列表”,默认情况下,当没有索引(html,htm,php asp ..)文件时,Apache有时会默认执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.