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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.