Created
September 30, 2013 10:54
-
-
Save driesd/6762162 to your computer and use it in GitHub Desktop.
Adds file size to a file field
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
//ADDS FILESIZE TO A FILE FIELD | |
function the_aim_theme_file_link($variables) { | |
$file = $variables['file']; | |
$icon_directory = $variables['icon_directory']; | |
$url = file_create_url($file->uri); | |
$icon = theme('file_icon', array('file' => $file, 'icon_directory' => $icon_directory)); | |
// Set options as per anchor format described at | |
// http://microformats.org/wiki/file-format-examples | |
$options = array( | |
'attributes' => array( | |
'type' => $file->filemime . '; length=' . $file->filesize, | |
), | |
); | |
// Use the description as the link text if available. | |
if (empty($file->description)) { | |
$link_text = $file->filename; | |
} | |
else { | |
$link_text = $file->description; | |
$options['attributes']['title'] = check_plain($file->filename); | |
} | |
return '<span class="file">' . $icon . ' ' . l($link_text, $url, $options) . ' (' . __format_file_fize_size($file->filesize) . ')</span>'; | |
} | |
//CUSTOM FUNCTION TO FORMAT FILESIZE FROM BYTES TO READABLE | |
function __format_file_fize_size( $size, $display_bytes=false ) { | |
if( $size < 1024 ) | |
$filesize = $size . ' bytes'; | |
elseif( $size >= 1024 && $size < 1048576 ) | |
$filesize = round( $size/1024, 2 ) . ' KB'; | |
elseif( $size >= 1048576 ) | |
$filesize = round( $size/1048576, 2 ) . ' MB'; | |
if( $size >= 1024 && $display_bytes ) | |
$filesize = $filesize . ' (' . $size . ' bytes)'; | |
return $filesize; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment