我如何使用php函数glob
从目录中获取文件并将它们放在一个数组中,如果它存在,则将它们包含在index.php?file=filename
中?
这是我到目前为止所提出的,但它不起作用。
$files = glob("files/*.php");
$file = array($files);
if (in_array(..
或者是否有一种更聪明的方法可以做到这一点,而无需在index.php
中写下所有页面?
答案 0 :(得分:0)
如果我理解你,你可以使用array_intersect()
:
$files = glob("files/*.php");
$allowedFiles = array('1.php', '2.php', '3.php')
$matchedFiles = array_intersect($files, $allowedFiles);
foreach ($matchedFiles as $file)
{
// do something with 'index.php?file=' . $file
}
不确定你的意思是“将它们包括在......”
答案 1 :(得分:0)
glob
返回一个数组,因此您无需对其执行任何其他操作。 docs中的示例显示类似于以下内容的内容:
foreach (glob("files/*.php") as $filename) {
echo "index.php?file=".basename($filename, ".php");
}
答案 2 :(得分:0)
我不知道如果我已经理解但是如果你想将所有文件放在一个数组中并将其发送到index.php页面,这就是这样做的方法:
$files = glob("files/*.php");
$group=array();
foreach($files as $file) $group[]="file[]=$file";
$path="index.php".(count($group) ? "?".implode("&",$group) : "");
答案 3 :(得分:0)
$files = glob("files/*.php");
foreach($files as $file) $group[]="index.php?page=$file";
现在在$ group数组中你有页面路径