TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Dumb question... (http://www.talkphp.com/absolute-beginners/1804-dumb-question.html)

Aaron 12-27-2007 05:01 PM

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?

xenon 12-27-2007 05:17 PM

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).

Dr John 12-27-2007 11:12 PM

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.

Salathe 12-27-2007 11:55 PM

Quote:

Originally Posted by Aaron (Post 7200)
When I do any php function that gets a size of something, does it measure the size in bytes? bits?

Functions like filesize return the size in bytes. The return values of all PHP functions are available in the PHP manual (there's always a section called "Return Values" with an explanation).

Quote:

Originally Posted by Aaron (Post 7200)
Does PHP have a built in function to convert these values, or do I have to write all that stuff?

It is very simple mathematics, essentially. There are hundreds of available functions available throughout the 'net which will take a byte value and convert it into any format that you want... but as a learning process I would recommend writing your own to do exactly what you want. All that are really needed are / (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.

Alan @ CIT 12-29-2007 11:16 PM

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