Skip to content

Instantly share code, notes, and snippets.

@lakinmohapatra
Created February 18, 2016 07:29
Show Gist options
  • Save lakinmohapatra/760892e5bbea85f71614 to your computer and use it in GitHub Desktop.
Save lakinmohapatra/760892e5bbea85f71614 to your computer and use it in GitHub Desktop.
Automated js/css files versioning in php
<?php
if (! function_exists('version')) {
/**
* Used to add file modified time for versioning css/js files.
*
* @param String $filePath - physical path of file.
* @return String FilePath
*/
function version($filePath = '')
{
if (file_exists($filePath)) {
return $filePath . '?version=' . filemtime($filePath);
}
return $filePath;
}
}
?>
<script type='text/javascript' src="<?php echo base_url(version('assets/js/Main.js')); ?>" ></script>
@mfsdebasis
Copy link

smart way of using filemtime :)
Extending to above, sometime the result of filemtime function are cached. In such case use clearstatcache() to clear the cache :)

@lakinmohapatra
Copy link
Author

Thanks Debasis for your comment :) . Will add clearstatcache() in order to clear stat cache.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment