Jump to content

filemtime returns folder date


EdMullen

Recommended Posts

I just noticed something while updating one of the sites I manage.

I use this code in the footer that is included in every page on the site:

<p>
<?php
$current_file_name = ($_SERVER['REQUEST_URI']);
$current_path = ($_SERVER['DOCUMENT_ROOT']);
$full_name = $current_path.$current_file_name;
$last_modified = filemtime($full_name);
print("This page last changed: ");
print(date("F j, Y - h:i A", $last_modified));
?>
</p>

However, here's what I found a tad odd.  If you go to:

http://westminsteratcrabapple.com/index.php

you get the correct modification date/time.  But, if you go to:

http://westminsteratcrabapple.com/

it shows a different date/time, which is of the folder, not imdex.php.

Any thoughts appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/277338-filemtime-returns-folder-date/
Share on other sites

Simple, $_SERVER['REQUEST_URI'] returns the value in the URL. If the URL includes the path to the file, then it includes the file. If the URL only includes the path to a folder and the webserver is picking a default file,then the file is not included in the value.

That point needs emphasis: it contains exactly everything in the URL starting with the path. That means it includes the path, "filename", and the query string. And whatever else I may decide to type in there.

 

What you're looking for is REQUEST_FILENAME or SCRIPT_FILENAME. That's an actual file.

 

[edit] Are you copy/pasting this code in all the files? Use the __FILE__ constant instead.

$last_modified = filemtime(__FILE__);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.