Maarten’s Posterous

Internet Development 
« Back to blog

Get filesize in PHP

Here's a function to get the size of a file on a server in PHP.

function get_filesize($url,$thereturn) {
    if (substr($url,0,4)=='http') {
        $x = array_change_key_case(get_headers($url, 1),CASE_LOWER);
        $x = $x['content-length'];
    } else {
        $x = @filesize($url);
    }
    if (!$thereturn) {
        return $x;
    } elseif($thereturn == 'mb') {
        return round($x / (1024*1024),2);
    } elseif($thereturn == 'kb') {
        return round($x / (1024),2);
    }
}

I'm not sure where I got this exactly, I'm guessting http://www.php.net. So credits to the original author.

Comments (0)

Leave a comment...

 
To leave a comment on this posterous, please login by clicking one of the following.
Posterous-login     Connect     twitter