Created
May 10, 2018 18:02
-
-
Save morgyface/7f4628dc86f87c059fe345bda4b3f143 to your computer and use it in GitHub Desktop.
WordPress | File size and extension
This file contains hidden or 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 | |
| $download_file = get_sub_field('download_file'); | |
| $download_url = $download_file['url']; | |
| $download_path = pathinfo($download_url); | |
| $download_ext = $download_path['extension']; | |
| $download_size = filesize( get_attached_file( $download_file['id'] ) ); | |
| function format_bytes($size, $precision = 2) { | |
| $base = log($size, 1024); | |
| $suffixes = array('', 'KB', 'MB', 'GG', 'TB'); | |
| return round(pow(1024, $base - floor($base)), $precision) .' '. $suffixes[floor($base)]; | |
| } | |
| $download_size = format_bytes($download_size); | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A more specific, versatile and recent file size function is available here