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,884 questions

51,810 answers

573 users

How to convert bytes to memory size (Bytes Or KB or MB or GB or TB or PB) in PHP

1 Answer

0 votes
function bytes_to_memory_size($bytes) {
    $memory_size = array('Bytes', 'KB', 'MB', 'GB', 'TB', 'PB');
 
    return round($bytes / pow(1024, ($i = floor(log($bytes,1024)))), 2) . ' ' . $memory_size[$i];
}
 
echo bytes_to_memory_size(723762) . "\n";
echo bytes_to_memory_size(92347347856) . "\n";
echo bytes_to_memory_size(998585739374626) . "\n";
echo bytes_to_memory_size(892374783738468723) . "\n";
echo bytes_to_memory_size(59883867) . "\n";
echo bytes_to_memory_size(467) . "\n";

 
 
/*
run:
 
706.8 KB
86.01 GB
908.21 TB
792.59 PB
57.11 MB
467 Bytes
 
*/

 



answered Mar 21, 2019 by avibootz
edited Jul 9, 2023 by avibootz
...