473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 7055
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: guillaume | last post by:
I have to read and process a large ASCII file containing a mesh : a list of points and triangles. The file is 100 MBytes. I first tried to do it in memory but I think I am running out of memory...
17
by: Sean Ross | last post by:
Hi. Recently I made a small script to do some file transferring (among other things). I wanted to monitor the progress of the file transfer, so I needed to know the size of the files I was...
49
by: Sam | last post by:
Hi all, Is there a function in the standard library that can get the size of a file? Thank you very much. Sam.
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
4
by: Doug | last post by:
Hi, It looks like the only way to get a size of a file within csharp is to use FileInfo and the Length property. However that only returns the number of bytes in the file which is translating...
2
by: Doug | last post by:
Hi, It looks like the only way to get a size of a file within dot net is to use FileInfo and the Length property. However that only returns the number of bytes in the file which is translating...
3
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
9
by: Matrixinline | last post by:
Hi All, Here is the problem char* memblock; std::ifstream file(sFileName, ios::in|ios::binary); if (file.is_open()) { size = file.tellg();
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.