列出所有带有PHP扩展名的文件作为链接列表

时间:2014-04-21 18:21:16

标签: php

我一直在尝试使用PHP生成动态站点地图,这是我已经拥有的:

<?php
   header ("Content-Type:text/xml");echo '<?xml version="1.0" encoding="UTF-8"?>
            <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

   //$url needs to link to the location of menu.inc on the server
   $url = $_SERVER['DOCUMENT_ROOT'] . "/assets/includes/menu.inc";
   $input = @file_get_contents($url) or die("Could not access file: $url");
   $regexp = "!\('[/a-zA-z0-9].+'\);!";

   if(preg_match_all($regexp, $input, $matches, PREG_SET_ORDER)) {
      foreach($matches as $match) {
         $match[0] = preg_replace("!\('/*!", "", $match[0]);
         $match[0] = preg_replace("!'\);!", "", $match[0]);
         echo '<url><loc>http://' . $_SERVER['SERVER_NAME'] . '/' . $match[0] . '</loc></url>';
      }
   }
   echo '</urlset>';
?>

它正在从menu.inc

中提取链接
<li><a <?php href('index.php');?>>Home</a></li>
<li><a <?php href('about.php');?>>About</a></li>
<li class="subMenu"><a <?php href('gallery.php');?>>Gallery</a>
    <ul>
        <li><a <?php href('services.php');?>>Services</a></li>
    </ul>
</li>
<li class="subMenu"><a class="noLink">Extras</a>
    <ul>
        <li><a <?php href('404.php');?>>404 Page</a></li>
        <li><a <?php href('blank.php');?>>Blank Page</a></li>
    </ul>
</li>
<li><a <?php href('contact.php');?>>Contact</a></li>

但是,我不希望它只列出菜单中的链接,因为某些链接可能位于页脚或网站页面中......所以我想改变概念在目录中将所有列出的文件拉为.php。

我发现了这个:PHP: Get list of all filenames contained within my images directory并且这个:PHP list of specific files in a directory很有帮助,并试图将这个想法融入我现在的想法中,但是失败了,请有人建议吗?

以下是我尝试过的内容:我试图使用它:

<?php
 if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != "..")
      {
            $thelist .= '<a href="'.$file.'">'.$file.'</a>';
          }
       }
  closedir($handle);
  }       
?>

然后基本上将$url = $_SERVER['DOCUMENT_ROOT'] . "/assets/includes/menu.inc";更改为$url = $thelist;

0 个答案:

没有答案
相关问题