steviez Posted November 22, 2007 Share Posted November 22, 2007 Hi, I am trying to show my users how much disk space out of their 100MB they have used. It has to be shown in a %. Here is my code so far: <?php $sql = "SELECT SUM(size) AS sum_size FROM `uploads` WHERE uploader = 'steviez' "; $result = mysql_query($sql) or die(mysql_error()); $i = mysql_fetch_array($result); $space_used = $i['sum_size']; $max space = '100MB'; function ByteSize($bytes) { $size = $bytes / 1024; if($size < 1024) { $size = number_format($size, 2); $size .= ' KB'; } else { if($size / 1024 < 1024) { $size = number_format($size / 1024, 2); $size .= ' MB'; } else if ($size / 1024 / 1024 < 1024) { $size = number_format($size / 1024 / 1024, 2); $size .= ' GB'; } } return $size; } $mb_used = ByteSize($space_used); ?> I have a percentage graph already but i just need to get php to tell it what percentage they have used. Please help Quote Link to comment Share on other sites More sharing options...
Orio Posted November 22, 2007 Share Posted November 22, 2007 Why don't you first compare the number of bytes the user used with the maximum bytes a user can use, and then format it into a string...? Orio. Quote Link to comment Share on other sites More sharing options...
steviez Posted November 22, 2007 Author Share Posted November 22, 2007 Im fairly new to PHP, could anyone help me with this? Quote Link to comment Share on other sites More sharing options...
Orio Posted November 22, 2007 Share Posted November 22, 2007 <?php $sql = "SELECT SUM(size) AS sum_size FROM `uploads` WHERE uploader = 'steviez' "; $result = mysql_query($sql) or die(mysql_error()); $i = mysql_fetch_array($result); $space_used = $i['sum_size']; $max_space = 100 * 1024 *1024; //100MB if($space_used > $max_space) echo "You are using too much!"; else echo "You are ok, you are using ".ByteSize($space_used)." out of allowed ".ByteSize($max_space); $mb_used = ByteSize($space_used); function ByteSize($bytes) { $size = $bytes / 1024; if($size < 1024) { $size = number_format($size, 2); $size .= ' KB'; } else { if($size / 1024 < 1024) { $size = number_format($size / 1024, 2); $size .= ' MB'; } else if ($size / 1024 / 1024 < 1024) { $size = number_format($size / 1024 / 1024, 2); $size .= ' GB'; } } return $size; } ?> Orio. Quote Link to comment Share on other sites More sharing options...
steviez Posted November 22, 2007 Author Share Posted November 22, 2007 That works perfect but only shows the use in MB. How would i make it return a % (eg you are using 50%) Thanks! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 22, 2007 Share Posted November 22, 2007 Just a simple bit of maths with the sizes: <?php $sql = "SELECT SUM(size) AS sum_size FROM `uploads` WHERE uploader = 'steviez' "; $result = mysql_query($sql) or die(mysql_error()); $i = mysql_fetch_array($result); $space_used = $i['sum_size']; $max_space = 100 * 1024 *1024; //100MB if($space_used > $max_space) echo "You are using too much!"; else echo "You are ok, you are using ".ByteSize($space_used)." out of allowed ".ByteSize($max_space)." which is ".number_format($space_used/$max_space*100,0)."% of your allowed space"; $mb_used = ByteSize($space_used); function ByteSize($bytes) { $size = $bytes / 1024; if($size < 1024) { $size = number_format($size, 2); $size .= ' KB'; } else { if($size / 1024 < 1024) { $size = number_format($size / 1024, 2); $size .= ' MB'; } else if ($size / 1024 / 1024 < 1024) { $size = number_format($size / 1024 / 1024, 2); $size .= ' GB'; } } return $size; } ?> Quote Link to comment Share on other sites More sharing options...
steviez Posted November 22, 2007 Author Share Posted November 22, 2007 THANK YOU!!!! Please could you PM me your rates for custom work? Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.