<?php

// ------------------------------------------------------------------------- //
// Espace disque                                                             //
// ------------------------------------------------------------------------- //
// Auteur: Emmanuel B <ebz@laposte.net>                                      //
// Web:    http://www.aaaelsm.com                                            //
// ------------------------------------------------------------------------- //

/*
Ce script permet de connaitre l'espace disque utilisé (base de données et
fichiers) chez son hébergeur.
*/
?>
<HTML>
<HEAD>
<TITLE>Espace disque utilis&eacute;</TITLE>
</HEAD>
<BODY>
<?php

$HOSTNAME
="localhost";
$USERNAME="root";
$PASSWORD="";
$DATABASE="test";
$maxtaille = 1024 * 40000;

$byteUnits = array('Octets', 'Ko', 'Mo', 'Go');

function
PMA_formatByteDown($value, $limes = 6, $comma = 0)
{
    
$dh           = pow(10, $comma);
    
$li           = pow(10, $limes);
    
$return_value = $value;
    
$unit         = $GLOBALS['byteUnits'][0];

    if (
$value >= $li*1000000) {
        
$value = round($value/(1073741824/$dh))/$dh;
        
$unit  = $GLOBALS['byteUnits'][3];
    }
    else if (
$value >= $li*1000) {
        
$value = round($value/(1048576/$dh))/$dh;
        
$unit  = $GLOBALS['byteUnits'][2];
    }
    else if (
$value >= $li) {
        
$value = round($value/(1024/$dh))/$dh;
        
$unit  = $GLOBALS['byteUnits'][1];
    }
    if (
$unit != $GLOBALS['byteUnits'][0]) {
        
$return_value = number_format($value, $comma,
            
$GLOBALS['number_decimal_separator'],
            
$GLOBALS['number_thousands_separator']);
    } else {
        
$return_value = number_format($value, 0,
            
$GLOBALS['number_decimal_separator'],
            
$GLOBALS['number_thousands_separator']);
    }

    return array(
$return_value, $unit);
}
// end of the 'PMA_formatByteDown' function

function rep($nom)
{
    
$nbTab=str_repeat("|----",substr_count($nom,"/"));
    
$nom1=eregi_replace("^[.][a-z0-9/ ]*/",$nbTab,$nom);
    echo
"<TR><TD align=\"left\">$nom1</TD>";
    global
$nbre;
    global
$tailletot;
    
$d = dir($nom);
    
$tailleRep=0;
    
$nbRep=0;
    while(
$entry=$d->read())
    {
        
$taille = filesize($nom . "/" . $entry);
        
$type = filetype($nom . "/" . $entry);
        if (
$type != "dir")
        {
            
$tailletot = $tailletot + $taille;
            
$nbre = $nbre + 1;
            
$tailleRep+=$taille;
            
$nbRep++;
        }
    }
    
$d = dir($nom);
    
$tailleRep=number_format($tailleRep,'0',',',' ');
    
$nbRep=number_format($nbRep,'0',',',' ');
    echo
"<TD align=\"right\">$tailleRep</TD>";
    echo
"<TD align=\"right\">$nbRep</TD></TR>";
    while(
$entry=$d->read())
    {
        
$taille = filesize($nom . "/" . $entry);
        
$type = filetype($nom . "/" . $entry);
        if (
$type == "dir")
        {
            if (
$entry != "." AND $entry != "..")
            {
                
$reps[]=$nom . "/" . $entry;
            }
        }
    }
    if (isset(
$reps)) {
        
natcasesort($reps);
        foreach (
$reps as $v) {
            
rep($v);
        }
    }
    
//rep($nom . "/" . $entry);
}

$db = mysql_connect($HOSTNAME, $USERNAME , $PASSWORD );
mysql_select_db($DATABASE,$db);

$local_query = "SHOW TABLE STATUS FROM $DATABASE";
$result      = mysql_query($local_query);
$nbTables=0;
if (
$result != FALSE && mysql_num_rows($result) > 0) {
    while (
$sts_tmp = mysql_fetch_array($result)) {
        
$tables[] = $sts_tmp;
        
$nbTables++;
    }
    
mysql_free_result($result);
}

?>
<TABLE cellspacing="20">
<TR><TH align="center" width="50%">Base de donn&eacute;es</TD>
<TH align="center" width="50%">Espace disque</TD></TR>
<TR><TD valign="top">
<TABLE>
<TR>
<TH align="center">Table</TH><TH align="right" width="80">Donn&eacute;es</TH>
<TH align="right" width="80">Index</TH><TH align="right" width="80">Total</TH>
</TR>
<?php
$td
=0;
$ti=0;
$s_tot=0;
while (list(
$keyname, $sts_data) = each($tables)) {
    list(
$sd,$sd_u)=PMA_formatByteDown($sts_data['Data_length'],3,1);
    
$sd="$sd $sd_u";
    
$td+=$sts_data['Data_length'];
    list(
$si,$si_u)=PMA_formatByteDown($sts_data['Index_length'],3,1);
    
$si="$si $si_u";
    
$ti+=$sts_data['Index_length'];
    list(
$sum,$sum_u)=PMA_formatByteDown(
                       
$sts_data['Data_length']+$sts_data['Index_length'],3,1);
    
$sum= "$sum $sum_u";;
    echo
"<TR><TD>".$sts_data['Name']."</TD><TD align='right'>$sd</TD>":
    echo
"<TD align='right'>$si</TD><TD align='right'>$sum</TD></TR>";
}
$ts=$td+$ti;
$s_tot=$ts;
list(
$td,$td_u)=PMA_formatByteDown($td,3,1);
$td="$td $td_u";
list(
$ti,$ti_u)=PMA_formatByteDown($ti,3,1);
$ti="$ti $ti_u";
list(
$ts,$ts_u)=PMA_formatByteDown($ts,3,1);
$ts="$ts $ts_u";
echo
"<TR><TD>Total</TD><TD align='right'>$td</TD><TD align='right'>$ti</TD>";
echo
"<TD align='right'>$ts</TD></TR>";
?>
</TABLE>
</TD><TD width="50%">
<TABLE cellpadding="0" cellspacing="0">
<pre>
<TABLE border="0" cellspacing="0">
<TR><TH align="center">R&eacute;pertoire</TH>
<TH align="right" width="80">Taille</TH>
<TH align="right" width ="60">Fichiers</TH></TR>
<?php
        $d
= dir(".");
        global
$nbre;
        global
$tailletot;
        
$nbre = 0;
        
$tailletot = 0;
        
rep(".");
?>
</TABLE>
<?php

$temp1
= number_format($nbre,'0',',',' ');
$temp2 = number_format($tailletot,'0',',',' ');
echo
"<BR>J'ai trouvé $temp1 fichiers d'une taille totale de $temp2 octets.<br>";
$temp1 = number_format($maxtaille - $tailletot,'0',',',' ');
echo
"<BR>Il vous reste $temp1 octets.<BR><BR>";
$s_tot+=$tailletot;
$s_libre=$maxtaille - $s_tot;
list(
$s_tot,$s_tot_u)=PMA_formatByteDown($s_tot,3,1);
$s_tot="$s_tot $s_tot_u";
list(
$s_libre,$s_libre_u)=PMA_formatByteDown($s_libre,3,1);
$s_libre="$s_libre $s_libre_u";

?>
</TD></TR>
<TR><TD colspan="2" align="center">
<B>
Espace utilis&eacute; : <?php echo $s_tot; ?><BR>
Espace libre : <?php echo $s_libre; ?>
</B>
</TD></TR></TABLE>
</BODY>
</HTML>