我想为我的网站做一个小的额外菜单。是否可以获取目录中的文件名称并将它们放在菜单中。
因此,如果你有一个包含文件的目录:facebook.php; twitter.php; stackoverflow.html; 你会得到这样的菜单:
- 脸书
- 唽
- 堆栈溢出
如果可能的话,我想选择他得到什么样的文件。所以我希望他得到.php和.html文件的名称,而不是.css文件的名称。
有人可以帮助我吗?
我喜欢glob():
foreach(glob("$dir/{*.php,*.html}", GLOB_BRACE) as $file) {
//whatever
}
或
$files = glob("$dir/{*.php,*.html}", GLOB_BRACE); //then use $files wherever
您可以将 pathinfo() 与 PATHINFO_BASENAME 一起使用以仅获取文件名PATHINFO_FILENAME获取它而不带扩展名。
尝试 scandir() 方法
在循环中检查当前文件是带有 is_dir() 的目录。然后你可以得到带有[dirname]/[dirname.php]的路径。检查是否存在文件,然后可以显示链接。
$files = scandir('menus');
$links = array();
foreach ($files as $file) {
if ($file == "." || $file == "..")
continue;
if (is_dir($file)) {
$menuFile = "./menus/$file/$file.php";
if (is_file($menuFile)) {
$links[$file] = $menuFile;
}
}
}
//display the links