简体   繁体   中英

How to give a .php file unique name at top of page, and display unique name on index.php file

I have a question and it's probably a simple one. What I would like to do is be able to place a unique name at the top of each .php file in a folder in the php code area. I would then like a script on the index.php file to look in that folder, pull out the unique names of each .php file found at the top of each page in the php code, and display them in a list on the index.php page.

Would I have to do something like this at the top of the page:

< ?
{{{{{uniquenamehere}}}}}
? >

And If so, what would the code look like for grabbing uniquenamehere and displaying in on the index.php page?

Thanks in advance, let me know if I need to be any more clear in my question. Sorry if it's a really simple question, I'm stumped!

EDIT

Getting this warning when using answer below: Warning: file_get_contents(test.php) [function.file-get-contents]: failed to open stream: No such file or directory in /path/index.php

Here's the code I am using,

  <?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/>";
}
}
?>

Not tested, but it should be roughly something like this.

<?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/>";
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM