如何在页面顶部指定.php文件的唯一名称,并在index.php文件上显示唯一名称
我有一个问题,可能很简单。我想做的是能够在php代码区域的文件夹中的每个.php文件的顶部放置一个唯一的名称。然后,我希望index.php文件上的一个脚本可以在该文件夹中查找,从php代码的每个页面顶部找到每个.php文件的唯一名称,并将它们显示在index.php页面的列表中 我是否需要在页面顶部执行类似操作:如何在页面顶部指定.php文件的唯一名称,并在index.php文件上显示唯一名称,php,string,variables,directory,Php,String,Variables,Directory,我有一个问题,可能很简单。我想做的是能够在php代码区域的文件夹中的每个.php文件的顶部放置一个唯一的名称。然后,我希望index.php文件上的一个脚本可以在该文件夹中查找,从php代码的每个页面顶部找到每个.php文件的唯一名称,并将它们显示在index.php页面的列表中 我是否需要在页面顶部执行类似操作: < ? {{{{{uniquenamehere}}}}} ? > 如果是这样,那么在这里抓取uniquename并显示在index.php页面上的代码会是什么样子
< ?
{{{{{uniquenamehere}}}}}
? >
<?
{{{{uniquenamehere}}}}
? >
如果是这样,那么在这里抓取uniquename并显示在index.php页面上的代码会是什么样子
提前谢谢,如果我需要更清楚地回答我的问题,请告诉我。如果这是一个非常简单的问题,我很抱歉,我被难住了
编辑
使用下面的答案时收到此警告:
警告:file_get_contents(test.php)[function.file get contents]:无法打开流:在/path/index.php中没有这样的文件或目录
这是我使用的代码
<?php
// Scan directory for files
$dir = "path/";
$files = scandir($dir);
// Iterate through the list of files
foreach($files as $file)
{
// Determine info about the file
$parts = pathinfo($file);
// If the file extension == php
if ( $parts['extension'] === "php" )
{
// Read the contents of the file
$contents = file_get_contents($file);
// Find first occurrence of opening template tag
$from = strpos($contents, "{{{{{");
// Find first occurrence of ending template tag
$to = strpos($contents,"}}}}}");
// Pull out the unique name from between the template tags
$uniqueName = substr($contents, $from+5, $to);
// Print out the unique name
echo $uniqueName ."<br/>";
}
}
?>
未测试,但大致应该是这样的
<?php
// Scan directory for files
$fileInfo = pathinfo(__FILE__);
$dir = $fileInfo['dirname'];
$files = scandir($dir);
// Iterate through the list of files
foreach($files as file)
{
// Determine info about the file
$parts = pathinfo($file);
// If the file extension == php
if ( $parts['extension'] == "php" )
{
// Read the contents of the file
$contents = file_get_contents($file);
// Find first occurrenceof opening template tag
$from = strpos($contents, "{{{{{");
// Find first occurrenceof ending template tag
$to = strpos($contents,"}}}}}");
// Pull out the unique name from between the template tags
$uniqueName = substr($contents, $from+5, $to);
// Print out the unique name
echo $uniqueName ."<br/>";
}
}
您不能只使用文件名吗?你想要的是可能的,但似乎是一个混乱的方法…给出一个例子“唯一的名称”,或几个例子。编辑:MySQL和它有什么关系?我同意@DaveRandom,只使用文件名。这将比打开和读取每个文件容易得多。这种方法并非前所未闻。CMS就像Wordpress一样,采用这种方法。模板文件的名称以特定格式定义在每个PHP文件的顶部。Wordpress然后解析文件列表并提取模板名称。此外,可能要使用的uniquename
KGDD可能包含不适合或不可在文件名中使用的字符;我在为没有.htaccess的顽固主机创建自己的目录列表时就这样做了。但与使用文件名相比,这是缓慢的。它在目录中查找文件,但没有在{{{{}}之间提取名称警告:file_get_contents(test.php)[function.file get contents]:无法打开流:在/path/index.phpIt中没有这样的文件或目录听起来您的$dir
路径与index.php的位置不同。我修改了上面的脚本以搜索脚本的当前目录,而不是任意目录。