disk_total_space

(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)

disk_total_spaceファイルシステムあるいはディスクパーティションの全体サイズを返す

説明

disk_total_space(string $directory): float|false

ディレクトリを含む文字列を指定してください。この関数は、 ファイルシステムまたはディスクパーティションに対応する全体バイト数を返します。

パラメータ

directory

ファイルシステムのディレクトリあるいはディスクパーティション。

戻り値

総バイト数を float 型で返します。 失敗した場合に false を返します。

例1 disk_total_space() の例

<?php
// $ds は、「/」で利用可能な全体バイト数
$ds = disk_total_space("/");

// Windows の場合:
$ds = disk_total_space("C:");
$ds = disk_total_space("D:");
?>

注意

注意: この関数では、 リモートファイル を 使用することはできません。これは、処理されるファイルがサーバーの ファイルシステムによりアクセスできる必要があるためです。

参考

  • disk_free_space() - ファイルシステムあるいはディスクパーティション上で利用可能な領域を返す

add a note

User Contributed Notes 5 notes

up
9
Viitala
17 years ago
Beware of empty files!

<?php

// Wrong
$exp = floor(log($bytes) / log(1024));

//Correct
$exp = $bytes ? floor(log($bytes) / log(1024)) : 0;

?>
up
8
tularis at php dot net
17 years ago
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))));
}
up
3
andudi at gmx dot ch
22 years ago
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
up
2
JulieC
18 years ago
"filesystem or disk partition" does not equal "directory" for Windows. Thanks.
up
2
stierguy1 at msn dot com
17 years ago
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]);}
To Top