{"id":297,"date":"2012-11-27T17:10:12","date_gmt":"2012-11-27T11:40:12","guid":{"rendered":"http:\/\/vikku.info\/codesnippets\/?p=297"},"modified":"2013-09-27T14:16:34","modified_gmt":"2013-09-27T08:46:34","slug":"format-filesize-as-bytes-kilobyte-megabyte-kb-mb-gb-ending-in-php","status":"publish","type":"post","link":"http:\/\/vikku.info\/codesnippets\/php\/format-filesize-as-bytes-kilobyte-megabyte-kb-mb-gb-ending-in-php\/","title":{"rendered":"format filesize as bytes kilobyte megabyte kb-mb-gb ending in php"},"content":{"rendered":"<p>I want to format a filesize in php as either of these bytes, kilobytes, megabytes, gigabytes according to the size of the file.<\/p>\n<p>I found the following codes in php.net comments section<\/p>\n<p>I just copied the following code as it was in the source and i have use only one and not the others.<\/p>\n<blockquote><p><strong>You can better directly do <a href=\"http:\/\/php.net\/manual\/en\/function.filesize.php\">this url<\/a>  to see the updated function or try the following functions.<\/strong><\/p><\/blockquote>\n<p>You can find these below the documentation of filesize function. (The comments section of the document).<\/p>\n<pre lang='php'>\r\nfunction FileSize($file, $setup = null)\r\n{\r\n    $FZ = ($file && @is_file($file)) ? filesize($file) : NULL;\r\n    $FS = array(\"B\",\"kB\",\"MB\",\"GB\",\"TB\",\"PB\",\"EB\",\"ZB\",\"YB\");\r\n    \r\n    if(!$setup && $setup !== 0)\r\n    {\r\n        return number_format($FZ\/pow(1024, $I=floor(log($FZ, 1024))), ($i >= 1) ? 2 : 0) . ' ' . $FS[$I];\r\n    } elseif ($setup == 'INT') return number_format($FZ);\r\n    else return number_format($FZ\/pow(1024, $setup), ($setup >= 1) ? 2 : 0 ). ' ' . $FS[$setup];\r\n}\r\n\r\n<\/pre>\n<p>Here is another one<\/p>\n<pre lang='php'>\r\n\r\nfunction format_bytes($size) {\r\n    $units = array(' B', ' KB', ' MB', ' GB', ' TB');\r\n    for ($i = 0; $size >= 1024 && $i < 4; $i++) $size \/= 1024;\r\n    return round($size, 2).$units[$i];\r\n}\r\n\r\n<\/pre>\n<p>and yet the simplest one as mentioned in the docs<\/p>\n<pre lang='php'>\r\n\r\nfunction format_bytes($bytes) {\r\n   if ($bytes < 1024) return $bytes.' B';\r\n   elseif ($bytes < 1048576) return round($bytes \/ 1024, 2).' KB';\r\n   elseif ($bytes < 1073741824) return round($bytes \/ 1048576, 2).' MB';\r\n   elseif ($bytes < 1099511627776) return round($bytes \/ 1073741824, 2).' GB';\r\n   else return round($bytes \/ 1099511627776, 2).' TB';\r\n}\r\n\r\n<\/pre>\n<p>besides, the document said <\/p>\n<p><strong>It's very interesting but I can't stand the decimals for bytes and KB so here's another example :<\/strong><\/p>\n<pre lang='php'>\r\n\r\nfunction format_size($size) {\r\n      $sizes = array(\" Bytes\", \" KB\", \" MB\", \" GB\", \" TB\", \" PB\", \" EB\", \" ZB\", \" YB\");\r\n      if ($size == 0) { return('n\/a'); } else {\r\n      return (round($size\/pow(1024, ($i = floor(log($size, 1024)))), $i > 1 ? 2 : 0) . $sizes[$i]); }\r\n}\r\n\r\n<\/pre>\n<p>and then another contributor said<\/p>\n<p><strong>This is a short and very clever function to get filesizes.<\/strong><\/p>\n<pre lang='php'>\r\n\r\nfunction format_size($size) {\r\n      $sizes = array(\" Bytes\", \" KB\", \" MB\", \" GB\", \" TB\", \" PB\", \" EB\", \" ZB\", \" YB\");\r\n      if ($size == 0) { return('n\/a'); } else {\r\n      return (round($size\/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]); }\r\n}\r\n\r\n<\/pre>\n<p>and it goes on like the above in http:\/\/php.net<\/p>\n<p><strong>Reference<\/strong><\/p>\n<p><a href=\"http:\/\/php.net\/manual\/en\/function.filesize.php\">http:\/\/php.net\/manual\/en\/function.filesize.php<\/a><\/p>\n<p>Scroll the page down to see as many as function like the above and suite yourself.<\/p>\n<p>Enjoy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I want to format a filesize in php as either of these bytes, kilobytes, megabytes, gigabytes according to the size of the file. I found the following codes in php.net comments section I just copied the following code as it was in the source and i have use only one and not the others. You [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/297"}],"collection":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/comments?post=297"}],"version-history":[{"count":4,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/297\/revisions"}],"predecessor-version":[{"id":315,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/297\/revisions\/315"}],"wp:attachment":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/media?parent=297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/categories?post=297"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/tags?post=297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}