Jump to content

[SOLVED] Percent Used


steviez

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/78403-solved-percent-used/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/78403-solved-percent-used/#findComment-396791
Share on other sites

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;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/78403-solved-percent-used/#findComment-396815
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.