As far as I know, WordPress has nothing built in for this, I would just do. Same thing did you also. So make sure that the $attachment_id
having value.
You can use custom function by place that function code in your function.php
file.
function getSize($file){
$bytes = filesize($file);
$s = array('b', 'Kb', 'Mb', 'Gb');
$e = floor(log($bytes)/log(1024));
return sprintf('%.2f '.$s[$e], ($bytes/pow(1024, floor($e))));
}
After place above function code, you can use getSize($url);
where ever you use into your template. Here $url
is reference of your file path.
Let me know if this stuff not help you.