XMLWriter handles XML escaping for URLs and produces well-formed markup. Avoid making the sitemap world-writable with chmod(..., 0777); give the PHP process only the permissions it actually needs.
<?php
$writer = new XMLWriter();
$writer->openMemory();
$writer->startDocument('1.0', 'UTF-8');
$writer->startElement('urlset');
$writer->writeAttribute('xmlns', 'https://www.sitemaps.org/schemas/sitemap/0.9');
foreach ($pdo->query('SELECT url, dt FROM file_dtl ORDER BY url') as $row) {
$writer->startElement('url');
$writer->writeElement('loc', $row['url']);
if (!empty($row['dt'])) {
$writer->writeElement('lastmod', $row['dt']);
}
$writer->endElement();
}
$writer->endElement();
$writer->endDocument();
$target = __DIR__ . '/sitemap.xml';
if (file_put_contents($target, $writer->outputMemory(), LOCK_EX) === false) {
throw new RuntimeException('Unable to write sitemap.xml');
}
?>
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"> <url>
<loc>https://www.example.com/</loc>
<lastmod>2013-01-01</lastmod>
<changefreq>monthly</changefreq>
<priority>0.7</priority>
</url>
</urlset>
In the above code the required data is URL and other data are optional . So we will use this data only to generate our xml file. <!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>Reading data from MySQL and generating sitemap XML file</title>
</head>
<body>
<?Php
include "z_db1.php";
//// Starting of xml file string ///
$body_content="<?xml version="1.0" encoding="UTF-8"?> n
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">n
"; // Store some text to enter inside the file
/////Reading from MySQL table //////
$sql="select url from file_dtl";
foreach ($dbo->query($sql) as $row) {
$body_content.="<url><loc>$row[url]</loc></url>n";
}
////// End of reading MySQL table //////
$body_content.="</urlset>"; // last part of the xml file
//// writing the content to xml file ////
$file_name="sitemap.xml"; // file name
$fp = fopen ($file_name, "w");
// Open the file in write mode, if file does not exist then it will be created.
fwrite ($fp,$body_content); // entering data to the file
fclose ($fp); // closing the file pointer
chmod($file_name,0777);
echo "Please check $file_name file ";
?>
</body>
</html>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.