Last active
August 25, 2022 20:55
-
-
Save Tonel/2f5fe86c8d34903496bb93a2b385a83e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Given a valid file location (it must be an path starting with "/"), i.e. "/css/style.css", | |
* it returns a string containing the file's mtime as query string, i.e. "/css/style.css?v=0123456789". | |
* Otherwise, it returns the file location. | |
* | |
* @param $file the file to be loaded. | |
*/ | |
function auto_version($file) { | |
// if it is not a valid path (example: a CDN url) | |
if (strpos($file, '/') !== 0 || !file_exists($_SERVER['DOCUMENT_ROOT'] . $file)) return $file; | |
// retrieving the file modification time | |
// https://www.php.net/manual/en/function.filemtime.php | |
$mtime = filemtime($_SERVER['DOCUMENT_ROOT'] . $file); | |
return sprintf("%s?v=%d", $file, $mtime); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
const CSS = array( | |
'/css/myCSSFile1.css', | |
'/css/myCSSFile2.css', | |
'/css/myCSSFile3.css', | |
); | |
foreach (CSS as $css) | |
echo '<link rel="stylesheet" href="' . auto_version($css) . '" type="text/css">'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
const JS = array( | |
'/js/myJsFile1.js', | |
'/js/myJsFile2.js', | |
'/js/myJsFile3.js', | |
); | |
foreach (JS as $js) | |
echo '<script src="' . auto_version($js) . '" type="text/javascript"></script>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment