Skip to content

Instantly share code, notes, and snippets.

@jrotering
Created April 9, 2013 14:53
Show Gist options
  • Save jrotering/5346347 to your computer and use it in GitHub Desktop.
Save jrotering/5346347 to your computer and use it in GitHub Desktop.
Displays the file size of a file in the local filesystem (relative to MODX_BASE_PATH) in bytes, kilobytes, or megabytes depending on its size.
<?php
$path = MODX_BASE_PATH . $file;
$fs = filesize($path);
if ($fs < 1024) {
return "${fs}B";
}
elseif ($fs < (1024*1024)) {
return round($fs / 1024, 0) .'KB';
}
elseif ($fs < 1073741824) {
return round($fs / 1048576, 1) . 'MB';
}
@jrotering
Copy link
Author

This is just quick-and-dirty; assumes path is relative to MODX_BASE_PATH, and the unit descriptors are hard-coded. Adjust as necessary.

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