PHP: disk_free_space - Manual

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: Sat, 27 Jan 2007

view this page in

# disk_free_space

(PHP 4 >= 4.1.0, PHP 5)

disk_free_space -- Получить размер доступного пространства в каталоге

## Описание

float disk_free_space ( string directory )

Функция возвращает размер свободного пространства в байтах, доступного для использования в указанном разделе диска.

Пример 1. Пример использования функции disk_free_space()

`<?php
$df = disk_free_space("/");
// $df содержит размер свободного места в каталоге "/"
// Под Windows:
disk_free_space("C:");
disk_free_space("D:");
?>`

> Замечание: Эта функция не применима для работы с удаленными файлами, поскольку файл должен быть доступен через файловую систему сервера.

См.также описание функции disk_total_space()

add a note User Contributed Notes
disk_free_space
djneoform at gmail dot com
20-Feb-2007 12:31
`Since I see this mistake all the time and don't understand why people keep making it, I'll explain.
1 Kilobyte is 1,000 bytes NOT 1,024 Bytes
Instead, 1,024 Bytes can be translated to 1 Kibibyte.
http://en.wikipedia.org/wiki/Mebibyte
There's no reason to be dividing by 1024 when calculating sizes since hard drive manufacturers don't use that method, instead they use 1000 bytes per Kilobyte.`
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`
add a note
disk_total_space dirname

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST