Jump to content

php sitemap script help - Last Mod Function not working properly


bomstudies
Go to solution Solved by CroNiX,

Recommended Posts

I have a simple php script that updates my sitemap automatically. However, when you click the link to the sitemap at www.bomstudies.com/sitemap-xml.php - the last mod date is showing 1969. The php script works for files that are not in sub folders. such as /talks or /commentary. I just need to know what snippet of code i should add, change, or delete to the sitemap php side of it to show the correct last mod date for my xml sitemap. I greatly appreciate your help. 

 

Thanks - 

 

Here is some of my code:

 

The Config Code: posted as config.php

<?php
/**
 * Change the configuration below and rename this file to config.php
 */
 
/*
 * The directory to check.
 * Make sure the DIR ends ups in the Sitemap Dir URL below, otherwise the links to files will be broken!
 */
define( 'SITEMAP_DIR', './');
 
// With trailing slash!
define( 'SITEMAP_DIR_URL', 'http://www.bomstudies.com/' );
 
// Whether or not the script should check recursively.
define( 'RECURSIVE', true );
 
// The file types, you can just add them on, so 'pdf', 'php' would work
$filetypes = array( 'php', 'html', 'pdf', 'doc', 'docx', 'xls', 'xlsx' );
 
// The replace array, this works as file => replacement, so 'index.php' => '', would make the index.php be listed as just /
$replace = array( 'index.php' => '' );
 
// The XSL file used for styling the sitemap output, make sure this path is relative to the root of the site.
$xsl = 'xml-sitemap.xsl';
 
// The Change Frequency for files, should probably not be 'never', unless you know for sure you'll never change them again.
$chfreq = 'weekly';
 
// The Priority Frequency for files. There's no way to differentiate so it might just as well be 1.
$prio = 1;
 
// Ignore array, all files in this array will be: ignored!
$ignore = array( 'config.php' );
 
 
the xml-sitemap.php page
<?php
 
/**
 * XML Sitemap PHP Script
 * Copyright ©, 2011 - 2012 - Joost de Valk, joost@yoast.com
 */
 
require './config.php';
 
// Get the keys so we can check quickly
$replace_files = array_keys( $replace );
 
// Sent the correct header so browsers display properly, with or without XSL.
header( 'Content-Type: application/xml' );
 
echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
 
$ignore = array_merge( $ignore, array( '.', '..', 'config.php', 'xml-sitemap.php' ) );
 
if ( isset( $xsl ) && !empty( $xsl ) )
echo '<?xml-stylesheet type="text/xsl" href="' . SITEMAP_DIR_URL . $xsl . '"?>' . "\n";
 
function parse_dir( $dir, $url ) {
global $ignore, $filetypes, $replace, $chfreq, $prio;
 
$handle = opendir( $dir );
while ( false !== ( $file = readdir( $handle ) ) ) {
 
// Check if this file needs to be ignored, if so, skip it.
if ( in_array( utf8_encode( $file ), $ignore ) )
continue;
 
if ( is_dir( $file ) ) {
if ( defined( 'RECURSIVE' ) && RECURSIVE )
parse_dir( $file, $url . $file . '/' );
}
 
// Check whether the file has on of the extensions allowed for this XML sitemap
$fileinfo = pathinfo( $dir . $file );
if ( in_array( $fileinfo['extension'], $filetypes ) ) {
 
// Create a W3C valid date for use in the XML sitemap based on the file modification time
if (filemtime( $dir .'/'. $file )==FALSE) {
$mod = date( 'c', filectime( $dir . $file ) );
} else {
$mod = date( 'c', filemtime( $dir . $file ) );
}
 
// Replace the file with it's replacement from the settings, if needed.
if ( in_array( $file, $replace ) )
$file = $replace[$file];
 
// Start creating the output
?>
 
    <url>
        <loc><?php echo $url . rawurlencode( $file ); ?></loc>
        <lastmod><?php echo $mod; ?></lastmod>
        <changefreq><?php echo $chfreq; ?></changefreq>
        <priority><?php echo $prio; ?></priority>
    </url><?php
}
}
closedir( $handle );
}
 
?>
parse_dir( SITEMAP_DIR, SITEMAP_DIR_URL );
?>
 
</urlset>
 
 
 
 
 
Link to comment
Share on other sites

The code that doesn't work seems to be in this part:

// Create a W3C valid date for use in the XML sitemap based on the file modification time
if (filemtime( $dir .'/'. $file )==FALSE) {
$mod = date( 'c', filectime( $dir . $file ) );
} else {
$mod = date( 'c', filemtime( $dir . $file ) );
}

First, it will try to use filemtime(). If it doesn't work, it'll try filectime. If filectime doesn't work, well, that's what you'll get anyway.

So, just try to debug your code and see what happens. Is it going to the 'if' or the 'else'?

 

The 'if' would be if the first filemtime() doesn't work. If it goes there, try to output the $mod variable and see what it is.

If it goes on the 'else' try to output the $mod variable.

 

Then, tell use what happens :)

Link to comment
Share on other sites

  • Solution

One of these lines is probably wrong.  Look at the $dir/$file concatenation, they're different with one adding a directory separator.

$fileinfo = pathinfo( $dir . $file );
if (filemtime( $dir .'/'. $file )==FALSE) {
Link to comment
Share on other sites

Thanks for your help! You were absolutely right - I changed these three lines of code as you suggested from the $dir to include the .'/' in the lines of code as follows:

 

// Create a W3C valid date for use in the XML sitemap based on the file modification time
if (filemtime( $dir .'/'. $file )==FALSE) {
$mod = date( 'c', filectime( $dir .'/' . $file ) );
} else {
$mod = date( 'c', filemtime( $dir .'/' . $file ) );
}
 
This updated the sitemap and the dates now read properly. This will be excellent in keeping the sitemap updating automatically and outputting the dates correctly as well. Hope this helps someone out in in the future.
 
BomStudies
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.