WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wanted to display the filesize of a document (pdf, word, excel etc) from a custom field. I already search and I found nothing. I got this code from other site but its in PHP. So I add it manually to my theme.

if($kpmFile !== get_post_custom_values("kpm_UploadFile")) {
$head = array_change_key_case(get_headers($kpmFile, TRUE));
echo $filesize = $head['content-length'];

I get this warning from above code:

Warning: get_headers() [function.get-headers]:

But if I make it like this:

if($kpmFile !== get_post_custom_values("kpm_UploadFile")) {
$head = array_change_key_case(get_headers("http://example.net/publications/Wellspring-of-Hope.pdf", TRUE));
echo $filesize = $head['content-length'];

The code will show the filesize in 44147474 which the file is actually 42.1MB

I need help on how to display the correct file size of the document in kb, mb or gb from custom field value.

Thanks in advance.

share|improve this question
44147474 / 1024^2 = 42.1 --> simply divide by 1024 twice to go from bytes to MB... – Johannes Pille Jul 12 '13 at 21:40
up vote 0 down vote accepted

function to handle to show file size i.e.

paste this code in functions.php

 * @param string $fileSize Filepath
 * @param int $digits Digits to display
 * @return string|bool Size (KB, MB, GB, TB) or boolean

function getFilesize($fileSize, $digits=2) {
    $sizes = array("TB","GB","MB","KB","B");
    $total = count($sizes);
    while ($total-- && $fileSize > 1024) {
        $fileSize /= 1024;
return round($fileSize, $digits)." ".$sizes[$total];

paste this code in single.php

if($kpmFile !== get_post_custom_values("kpm_UploadFile")) {
$head = array_change_key_case(get_headers("http://example.net/publications/Wellspring-of-Hope.pdf", TRUE));
echo $filesize = getFilesize($head['content-length']);
share|improve this answer
Thanks Ravs, sorry I forgot to mention that I am not good in PHP. I actually stumbled to that code but I dont know how to integrate the custom field. – MightyGas Jul 12 '13 at 19:19
answer updated check it and give a try – Ravinder Kumar Jul 12 '13 at 19:25
It display blank. Correct me if im wrong, the $_SERVER["DOCUMENT_ROOT"].$filePath; will display as "var/home/etc" right? but in my customfield is in "example.com/thefile.pdf"; Maybe thats is why in display blank? Thanks – MightyGas Jul 12 '13 at 19:36
$kpmFile is it filepath url? print the output print_r($kpmFile) and share the result – Ravinder Kumar Jul 12 '13 at 19:42
Yes the $kpmFile is the file path. I tried print_r still show blank. :( – MightyGas Jul 12 '13 at 20:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.