PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

disk_total_space> <dirname
Last updated: Fri, 10 Oct 2008

view this page in

disk_free_space

(PHP 4 >= 4.0.7, PHP 5)

disk_free_spaceRenvoie l'espace disque disponible dans le répertoire

Description

float disk_free_space ( string $directory )

Renvoie l'espace disque disponible dans le répertoire ou la partition.

Liste de paramètres

directory

Un dossier du système de fichier ou une partition d'un disque.

Note: Si vous fournissez un fichier au lieu d'un dossier, le comportement de cette fonction peut être aléatoire, suivant le système d'exploitation et les versions de PHP.

Valeurs de retour

Retourne le nombre d'octets disponible, sous la forme d'un nombre à virgule flottante.

Exemples

Exemple #1 Exemple avec disk_free_space()

<?php
// $df contient le nombre d'octets libres sur "/"
$df disk_free_space("/");

// Sous Windows:
disk_free_space("C:");
disk_free_space("D:");
?>

Notes

Note: Cette fonction ne fonctionne pas avec les fichiers distants, car le fichier utilisé doit être accessible sur le système de fichiers local.

Voir aussi



disk_total_space> <dirname
Last updated: Fri, 10 Oct 2008
 
add a note add a note User Contributed Notes
disk_free_space
rostvertol dot mil at gmail dot com
18-Feb-2008 11:27
A cleaner and more efficient way of making human readable file sizes:

<?php
function decodeSize( $bytes )
{
   
$types = array( 'B', 'KB', 'MB', 'GB', 'TB' );
    for(
$i = 0; $bytes >= 1024 && $i < ( count( $types ) -1 ); $bytes /= 1024, $i++ );
    return(
round( $bytes, 2 ) . " " . $types[$i] );
}
?>
root at mantoru dot de
06-Dec-2007 08:25
Note that disk_free_space() does an open_basedir check.
Nitrogen
10-Jan-2007 05:50
Another easy way to convert bytes to human readable sizes would be this:

<?php
function HumanSize($Bytes)
{
 
$Type=array("", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta");
 
$Index=0;
  while(
$Bytes>=1024)
  {
   
$Bytes/=1024;
   
$Index++;
  }
  return(
"".$Bytes." ".$Type[$Index]."bytes");
}
?>

It simply takes the $Bytes and divides it by 1024 bytes untill it's no longer over or equal to 1024, meanwhile it increases the $Index to allocate which suffix belongs to the return (adding 'bytes' to the end to save some space).
You can easily modify it so it's shorter, but I made it so it's more clearer.

Nitrogen.
mixar at yandex dot ru
04-Dec-2006 03:33
This the right function is:

function formatSize($size){
    switch (true){
    case ($size > 1099511627776):
        $size /= 1099511627776;
        $suffix = 'TB';
    break;
    case ($size > 1073741824):
        $size /= 1073741824;
        $suffix = 'GB';
    break;
    case ($size > 1048576):
        $size /= 1048576;
        $suffix = 'MB';   
    break;
    case ($size > 1024):
        $size /= 1024;
        $suffix = 'KB';
        break;
    default:
        $suffix = 'B';
    }
    return round($size, 2).$suffix;
}
djneoform at gmail dot com
12-Jul-2006 07:13
List all drives, free space, total space and percentage free.

<?
    for ($i = 67; $i <= 90; $i++)
    {
        $drive = chr($i);
        if (is_dir($drive.':'))
        {
            $freespace             = disk_free_space($drive.':');
            $total_space         = disk_total_space($drive.':');
            $percentage_free     = $freespace ? round($freespace / $total_space, 2) * 100 : 0;
            echo $drive.': '.to_readble_size($freespace).' / '.to_readble_size($total_space).' ['.$percentage_free.'%]<br />';
        }
    }

    function to_readble_size($size)
    {
        switch (true)
        {
            case ($size > 1000000000000):
                $size /= 1000000000000;
                $suffix = 'TB';
                break;
            case ($size > 1000000000):
                $size /= 1000000000;
                $suffix = 'GB';
                break;
            case ($size > 1000000):
                $size /= 1000000;
                $suffix = 'MB';   
                break;
            case ($size > 1000):
                $size /= 1000;
                $suffix = 'KB';
                break;
            default:
                $suffix = 'B';
        }
        return round($size, 2).$suffix;
    }
?>
Ashraf M Kaabi
01-Mar-2005 08:38
and also you can know the used space , in this
example :
<?
function disk_used_space($drive)
{
    return disk_total_space("$drive:") - disk_free_space("$drive:");
}

echo disk_used_space('C');
?>
aidan at php dot net
15-Oct-2004 05:49
To make human readable file sizes, see this function:

http://aidanlister.com/repos/v/function.size_readable.php

disk_total_space> <dirname
Last updated: Fri, 10 Oct 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites