![]() |
Dumb question...
When I do any php function that gets a size of something, does it measure the size in bytes? bits?
and the size ratio is like this, correct?: 1 kilobyte (KB) 1,024 bytes 1 megabyte (MB) 1,048,576 bytes 1 gigabyte (GB) 1,073,741,824 bytes 1 terabyte (TB) 1,099,511,627,776 bytes 1 petabyte (PB) 1,125,899,906,842,624 bytes can also be seen as this: Bit: Smallest unit of measurement. Byte: 8 bits Kilobyte: (KB) 1024 bytes. MegaByte: (MB) 1024 kilobytes (KB) GigaByte: (GB) 1024 megabytes (MB) but is the size really important? Could you round it down and say 1kb = 1000 bytes? Does PHP have a built in function to convert these values, or do I have to write all that stuff? |
No, PHP doesn't have what you want, but you could easily write your own functions (it's actually pretty easy). All you have to do is some basic math. Every next measurement unit is the previous measurement unit * 1024.
- 1 kilobyte (KB) = 1,024 bytes - 1 megabyte (MB) = 1 KB * 1024 = 1024 * 1024 = 1,048,576 bytes - 1 gigabyte (GB) = 1MB * 1024 = ( 1024 * 1024 ) * 1024 = 1,073,741,824 bytes And no, you can't 'just round down the values to 1000'. In programming, that doesn't work - by doesn't work I mean you won't get the right values. In marketing, it does (see the storage devices). |
Does it measure the size in bytes??? The size of WHAT? Depends what your function is going to be used for - it could be pixels, it could be inches, it could be light years, it could be moles - it's your function, you decide.
|
Quote:
Quote:
/ (divide), (int) (type casting/juggling) or intval() (convert number to integer) -- or if you're feeling confident you could use shifting bitwise operators ;)Also, on the topic of 1000 bytes versus 1024 bytes, two different systems are available for use. Using SI prefix, 1,000,000 bytes equals 1 megabyte (MB); using Binary prefix, 1,048,576 (1024 * 1024) bytes equals 1 mebibyte (MiB). Often, notably, the SI prefixes are used for both instances with the precise meaning being determined by the context in which the value is used! More information on this ambiguity can be found on Wikipedia. |
Here is a function I've used on a few projects (because I'm too lazy to write my own :D) which does a fine job of converting a filesize() result (bytes) to a "human readable" size
PHP: filesize - Manual Alan. |
| All times are GMT. The time now is 02:20 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0