PHP Function: Get a file’s last modified timestamp

I’ve recently found the need to show a file’s last modified timestamp. This function is useful when including CSS or JavaScript files. By including a query string will ensure web browsers re-download a fresh copy of the file rather than using a cached file.

Simply run this function as you’re including a file, such as:

The example above will add style.css’ last modified timestamp as a query string, therefore each time the style.css is updated; the timestamp will be updated forcing the web browser to download a new copy. You will need to include this function code for the above code to work properly:

You will have three options when using this function:

  • $filename = This is the filename of the item you wish to check. E.g. “style.css”.
  • $filepath = You can use this variable to set the filepath of the above filename. Such as “css/”.
  • $displayV = This option has a default value of “false”. You can change this variable to “true”, doing so will prepend “?v=” before the timestamp, so your final timestamp will look like: “?v=1438124400”.

The final result of code with look very similar to:

There are no comments, be the first. Use the form below to add your comment

Post a Comment