disk_total_space
(PHP 4 >= 4.1.0, PHP 5)
disk_total_space — Returns the total size of a filesystem or disk partition
Description
float disk_total_space
( string
$directory
)Given a string containing a directory, this function will return the total number of bytes on the corresponding filesystem or disk partition.
Parameters
-
directory
-
A directory of the filesystem or disk partition.
Return Values
Returns the total number of bytes as a float
or FALSE
on failure.
Examples
Example #1 disk_total_space() example
<?php
// $ds contains the total number of bytes available on "/"
$ds = disk_total_space("/");
// On Windows:
$ds = disk_total_space("C:");
$ds = disk_total_space("D:");
?>
Notes
Note: This function will not work on remote files as the file to be examined must be accessible via the server's filesystem.
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с файловой системой
- Функции для работы с файловой системой
- 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
- parse_ini_string
- pathinfo
- pclose
- popen
- readfile
- readlink
- realpath_cache_get
- realpath_cache_size
- realpath
- rename
- rewind
- rmdir
- set_file_buffer
- stat
- symlink
- tempnam
- tmpfile
- touch
- umask
- unlink
Коментарии
To find the total size of a file/directory you have to differ two situations:
(on Linux/Unix based systems only!?)
you are interested:
1) in the total size of the files in the dir/subdirs
2) what place on the disk your dir/subdirs/files uses
- 1) and 2) normaly differs, depending on the size of the inodes
- mostly 2) is greater than 1) (in the order of any kB)
- filesize($file) gives 1)
- "du -ab $file" gives 2)
so you have to choose your situation!
on my server I have no rights to use "exec du" in the case of 2), so I use:
$s = stat($file);
$size = $s[11]*$s[12]/8);
whitch is counting the inodes [12] times the size of them in Bits [11]
hopes this helps to count the used disk place in a right way... :-)
Andreas Dick
"filesystem or disk partition" does not equal "directory" for Windows. Thanks.
function roundsize($size){
$i=0;
$iec = array("B", "Kb", "Mb", "Gb", "Tb");
while (($size/1024)>1) {
$size=$size/1024;
$i++;}
return(round($size,1)." ".$iec[$i]);}
For a non-looping way to add symbols to a number of bytes:
<?php
function getSymbolByQuantity($bytes) {
$symbols = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
$exp = floor(log($bytes)/log(1024));
return sprintf('%.2f '.$symbol[$exp], ($bytes/pow(1024, floor($exp))));
}
Beware of empty files!
<?php
// Wrong
$exp = floor(log($bytes) / log(1024));
//Correct
$exp = $bytes ? floor(log($bytes) / log(1024)) : 0;
?>
Something that might go well with this function is the ability to list available disks. On Windows, here's the relevant code:
<?php
/**
* Finds a list of disk drives on the server.
* @return array The array velues are the existing disks.
*/
function get_disks(){
if(php_uname('s')=='Windows NT'){
// windows
$disks=`fsutil fsinfo drives`;
$disks=str_word_count($disks,1);
if($disks[0]!='Drives')return '';
unset($disks[0]);
foreach($disks as $key=>$disk)$disks[$key]=$disk.':\\';
return $disks;
}else{
// unix
$data=`mount`;
$data=explode(' ',$data);
$disks=array();
foreach($data as $token)if(substr($token,0,5)=='/dev/')$disks[]=$token;
return $disks;
}
}
?>
EXAMPLE OF USE:
<?php print_r(get_disks()); ?>
EXAMPLE RESULT:
Array
(
[1] => A:\
[2] => C:\
[3] => D:\
[4] => E:\
[5] => F:\
[6] => G:\
[7] => H:\
[8] => I:\
[9] => M:\
[10] => X:\
[11] => Z:\
)
Warning: This also finds empty disk drives (eg; CD or SMD drives or the more common floppy drive).
Warning2: If you want to find space usage using the info from my function, prepend the disk function with the "@", eg:
$free=@disk_free_space('A:\\');
<?php
//This is a more readable way of viewing the returned float
// $Bytes contains the total number of bytes on "/"
$Bytes = disk_total_space("/");
function dataSize($Bytes)
{
$Type=array("", "kilo", "mega", "giga", "tera");
$counter=0;
while($Bytes>=1024)
{
$Bytes/=1024;
$counter++;
}
return("".$Bytes." ".$Type[$counter]."bytes");
}
?>