470,870 Members | 1,387 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

File Size Conversion

Can anybody help me out by writing a function for the given problem

Write a function in PHP that converts given number of bytes into
Readabel format like 99.92 KB for 102323 bytes and
3 MB for 3072 bytes and so on. Let your function be
sophasticated,portable,easy readable.

Thankzz in advance
Napoleon

Dec 19 '05 #1
7 6909
Following on from Prince of Code's message. . .
You seem to have posted to comp.lang.php by mistake
I think you meant comp.rtfm or possibly comp.domy.coursework

Can anybody help me out by writing a function for the given problem

Write a function in PHP that converts given number of bytes into
Readabel format like 99.92 KB for 102323 bytes and
3 MB for 3072 bytes and so on. Let your function be
sophasticated,portable,easy readable.

Thankzz in advance
Napoleon


--
PETER FOX Not the same since the exam marking business failed
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Dec 19 '05 #2
I dont understand anything

Peter Pls give something if relavant and usefull

Thankzz
Prince of Code

Dec 19 '05 #3
"Prince of Code" wrote:
I dont understand anything


No shit.

One of these days I'm going to start filtering out everything from Google
Groups. :-(

--
phil [dot] ronan @ virgin [dot] net
http://vzone.virgin.net/phil.ronan/

Dec 19 '05 #4
Hi Prince of Code,

What you are asking is quite simple. But with a name like Prince Of Code
it looks like you're too lazy to do the ground work and learn the basics
of the language.

I am sure you'll appreciate that we have our own projects to complete
and so can't hand hold people through every step of the learning process.

You might want to look at a simple function like this though (untested)

function readableFilesize($bytes){
switch(TRUE){
case ($bytes<1024)
return ($bytes . "bytes");
break;
case ($bytes>1023)
return ($bytes/1024 . "KB");
break;
case ($bytes>104858)
return ($bytes/1000000 . "MB");
break;
}
}
now, I'm not doing the rest for you, you need to look for someway to
round the answers and add it into the returns

www.php.net and look for rounding functions.

HTH - but please do some work yourself before posting!

Rich
Prince of Code wrote:
Can anybody help me out by writing a function for the given problem

Write a function in PHP that converts given number of bytes into
Readabel format like 99.92 KB for 102323 bytes and
3 MB for 3072 bytes and so on. Let your function be
sophasticated,portable,easy readable.

Thankzz in advance
Napoleon

Dec 19 '05 #5
1 KB == 1024 bytes
rest you can figure out yourself

--
Geeks Home
www.fahimzahid.com


"Prince of Code" <pr**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Can anybody help me out by writing a function for the given problem

Write a function in PHP that converts given number of bytes into
Readabel format like 99.92 KB for 102323 bytes and
3 MB for 3072 bytes and so on. Let your function be
sophasticated,portable,easy readable.

Thankzz in advance
Napoleon

Dec 22 '05 #6
Here's a function a wrote for a previous program:

/****************************
* File size unit converter *
****************************/
function fsize_unit_convert($bytes)
{
$units = array('b', 'kb', 'mb', 'gb');
$converted = $bytes . ' ' . $units[0];
for ($i = 0; $i < count($units); $i++)
{
if (($bytes/pow(1024, $i)) >= 1)
{$converted = round($bytes/pow(1024, $i), 2) . ' ' . $units[$i];}
}
return $converted;
}

Java Boy wrote:
1 KB == 1024 bytes
rest you can figure out yourself

--
Geeks Home
www.fahimzahid.com


"Prince of Code" <pr**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Can anybody help me out by writing a function for the given problem

Write a function in PHP that converts given number of bytes into
Readabel format like 99.92 KB for 102323 bytes and
3 MB for 3072 bytes and so on. Let your function be
sophasticated,portable,easy readable.

Thankzz in advance
Napoleon


--
http://www.uranther.com
Jan 5 '06 #7
This is what I use.
Maybe not so complicated, but more easy readable. It gives 0.6Mb instead of
600 kb etc Off course you can expand this with Gb etc...

if ($filesize < 500)
{
$filesize = "$grootte b";
}
elseif ($filesize <((1024 * 1024)/2))
{
$filesize = round ($filesize / 1024,1);
$filesize = "$filesize Kb";
}
else
{
$filesize = round (($filesize / 1024 / 1024),2);
$filesize = "$filesize Mb";
}
"James Wheaton" <ap******@yahoo.com> schreef in bericht
news:cZ_uf.685049$_o.478491@attbi_s71...
Here's a function a wrote for a previous program:

/****************************
* File size unit converter *
****************************/
function fsize_unit_convert($bytes)
{
$units = array('b', 'kb', 'mb', 'gb');
$converted = $bytes . ' ' . $units[0];
for ($i = 0; $i < count($units); $i++)
{
if (($bytes/pow(1024, $i)) >= 1)
{$converted = round($bytes/pow(1024, $i), 2) . ' ' . $units[$i];}
}
return $converted;
}

Java Boy wrote:
1 KB == 1024 bytes
rest you can figure out yourself

--
Geeks Home
www.fahimzahid.com


"Prince of Code" <pr**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Can anybody help me out by writing a function for the given problem

Write a function in PHP that converts given number of bytes into
Readabel format like 99.92 KB for 102323 bytes and
3 MB for 3072 bytes and so on. Let your function be
sophasticated,portable,easy readable.

Thankzz in advance
Napoleon


--
http://www.uranther.com

Jan 5 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by guillaume | last post: by
17 posts views Thread by Sean Ross | last post: by
49 posts views Thread by Sam | last post: by
4 posts views Thread by Doug | last post: by
2 posts views Thread by Doug | last post: by
9 posts views Thread by Matrixinline | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.