How can I convert the output of PHP's filesize() function to a nice format with MegaBytes, KiloBytes etc?

like:
  • if the size is less than 1 MB, show the size in KB
  • if it's between 1 MB - 1 GB show it in MB
  • [*] if it's larger - in GB