Create Site Map Tutorial - Page 5
Create Site Map Tutorial - Page 1
Create Site Map Tutorial - Page 2
Create Site Map Tutorial - Page 3
Create Site Map Tutorial - Page 4
Now for the rest of the program - first a function to call the walkDirTree function, then sort the $files array. Everything else is used to write out the $files array to the sitemap.xml file along with some sillyness that is really un-neccessary, but I wanted to see what was going on.
Notice that for my site, I open '../sitemap.xml' - that is because I keep my script in a folder off the root, but I want the xml file in the root of my web site. That may need to be adjusted for your needs.
The rest of the code is used to time the event - typical times on this site for 100 or so pages are less than 2 seconds to generate the xml file.
{
list($msec, $sec) = explode(" ", microtime());
$time = $sec . substr($msec, 1);
return $as_float === false ? $time : (float)$time;
}
$starttime = myMicrotime(TRUE);
function walkTree() {global $files, $sitepath;
walkDirTree($sitepath);
sort($files);
}
//Main Code starts here
error_reporting(0);
echo "$serverTalk Hello, how may I help you?</span><br />";
echo "<br />User @ $siteurl: I want you to generate a new SiteMap.XML file!<br />";
echo "$serverTalk Ok, hang on while I create a new Google® sitemap.xml file</span><br /><hr /><b>";
walkTree();
if ($fp = fopen("../sitemap.xml", "w")) {
echo "</b><hr />$serverTalk Writing sitemap.xml file to server ... </span>";
fputs($fp, '<?xml version="1.0" encoding="UTF-8"?>'."\n");
fputs($fp, '<!--Google Site Map File of '."$siteurl generated on ".date("D, d M Y G:i:s T").' -->'."\n");
fputs($fp, '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">'."\n");
foreach ($files as $url) {
fputs($fp, "<url>\n\t");
fputs($fp, "<loc>$url[file]</loc>\n\t");
if($lastmodification == true) {
fputs($fp, "<lastmod>".date('Y-m-d', $url[ftime])."</lastmod>\n\t");
}
fputs($fp, "<changefreq>$freq</changefreq>\n\t");
fputs($fp, "<priority>$priority</priority>\n");
fputs($fp, "</url>\n");
}
fputs($fp, "</urlset>");
fclose($fp);
$endtime = myMicrotime(TRUE);
echo "$serverTalk Ok, Master - I did ",count($files)," URLs in ",$endtime-$starttime," seconds!</span>";
}
else {
echo "</b><hr />$serverTalk I am Sorry Master - I encountered an Error: Could not write to 'sitemap.xml' file.</span>";
}
echo "<p><a href='sitemap2.php'>View New Site Map</a></p>";
echo "<p><a href='$siteurl'>Return to Home Page</a></p>";
echo "<p><a href='".$siteurl."sitemap.shtml'>Return to Site Map Page</a></p>";
}
?>