Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Filesystem Functions : disk_free_space

disk_free_space

Returns available space in directory (PHP 4 >= 4.0.7, PHP 5)
float disk_free_space ( string directory )

Given a string containing a directory, this function will return the number of bytes available on the corresponding filesystem or disk partition.

Parameters

directory

A directory of the filesystem or disk partition.

Note:

Given a file name instead of a directory, the behaviour of the function is unspecified and may differ between operating systems and PHP versions.

Return Values

Returns the number of available bytes as a float.

Examples

Example 622. disk_free_space() example

<?php
// $df contains the number of bytes available on "/"
$df = disk_free_space("/");

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


Notes

Note:

This function will not work on remote files as the file to be examined must be accessible via the servers filesystem.

Related Examples ( Source code ) » disk_free_space


Code Examples / Notes » disk_free_space

aidan

To make human readable file sizes, see this function:
http://aidanlister.com/repos/v/function.size_readable.php


mixar

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

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


nitrogen

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.


ashraf m kaabi

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');
?>


Change Language


Follow Navioo On Twitter
basename
chgrp
chmod
chown
clearstatcache
copy
delete
dirname
disk_free_space
disk_total_space
diskfreespace
fclose
feof
fflush
fgetc
fgetcsv
fgets
fgetss
file_exists
file_get_contents
file_put_contents
file
fileatime
filectime
filegroup
fileinode
filemtime
fileowner
fileperms
filesize
filetype
flock
fnmatch
fopen
fpassthru
fputcsv
fputs
fread
fscanf
fseek
fstat
ftell
ftruncate
fwrite
glob
is_dir
is_executable
is_file
is_link
is_readable
is_uploaded_file
is_writable
is_writeable
lchgrp
lchown
link
linkinfo
lstat
mkdir
move_uploaded_file
parse_ini_file
pathinfo
pclose
popen
readfile
readlink
realpath
rename
rewind
rmdir
set_file_buffer
stat
symlink
tempnam
tmpfile
touch
umask
unlink
eXTReMe Tracker