Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,880 questions

51,806 answers

573 users

How to convert Bytes Or KB or MB or GB or TB or PB to bytes in PHP

1 Answer

0 votes
function get_bytes($size, $datasize) {
    $types = array("B", "KB", "MB", "GB", "TB", "PB"); 
     
    if ($key = array_search($datasize, $types))
        return $size * pow(1024, $key);
         
    else return "invalid type";
  }
   
echo "7 MB = " . number_format(get_bytes(7, "MB"), 0, ',') . " bytes \n";
echo "3 KB = " . number_format(get_bytes(3, "KB"), 0, ',') . " bytes \n";
echo "1 GB = " . number_format(get_bytes(1, "GB"), 0, ',') . " bytes \n";
echo "2 TB = " . number_format(get_bytes(2, "TB"), 0, ',') . " bytes \n";
  
echo get_bytes(4, "XB"); 
 
  
 
  
/*
run: 
  
7 MB = 7,340,032 bytes 
3 KB = 3,072 bytes 
1 GB = 1,073,741,824 bytes 
2 TB = 2,199,023,255,552 bytes 
invalid type
  
*/

 



answered Jul 9, 2023 by avibootz
edited Jul 9, 2023 by avibootz
...