0

I have been trying to generate a dynamic sitemap using PHP, here is what I already have:

<?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>';
?>

It is pulling the links from 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>

However, I don't want it to only list down the links that are in the menu, as some links might be in the footer or within the pages of the website... So I wanted to change the concept to pull all the files listed as .php within the directory instant.

I have found this: PHP: Get list of all filenames contained within my images directory and this: PHP list of specific files in a directory helpful, and tried to integrate the idea to what I currently have, but failed miserably, please can anyone advice?

Here is what I have tried:, I have tried to use this:

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

and then basically change $url = $_SERVER['DOCUMENT_ROOT'] . "/assets/includes/menu.inc"; to $url = $thelist;

Community
  • 1
  • 1
Leo
  • 967
  • 2
  • 14
  • 37
  • Please show how you tried to integrate the ideas of those answers, so we can help you understand what you did wrong. – Barmar Apr 21 '14 at 18:23
  • Thank you, please check the last bit of the question. – Leo Apr 21 '14 at 18:31
  • You don't seem to be checking the extension. Maybe use the `glob()` function to get a list of all files matching a wildcard. – Barmar Apr 21 '14 at 18:36

0 Answers0