日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

space index.php 7-14,disk_free_space()

發(fā)布時(shí)間:2023/12/9 php 80 豆豆
生活随笔 收集整理的這篇文章主要介紹了 space index.php 7-14,disk_free_space() 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

disk_free_space()

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

返回目錄中的可用空間

說明disk_free_space(string$directory):float

給出一個(gè)包含有一個(gè)目錄的字符串,本函數(shù)將根據(jù)相應(yīng)的文件系統(tǒng)或磁盤分區(qū)返回可用的字節(jié)數(shù)。

參數(shù)$directory文件系統(tǒng)目錄或者磁盤分區(qū)。Note:

如果指定了文件名而不是文件目錄,這個(gè)函數(shù)的行為將并不統(tǒng)一,會(huì)因操作系統(tǒng)和 PHP 版本而異。

返回值

以浮點(diǎn)返回可用的字節(jié)數(shù),或者在失敗時(shí)返回FALSE。

范例

Example #1disk_free_space()例子<?php

// $df 包含根目錄下可用的字節(jié)數(shù)

$df = disk_free_space("/");

//在 Windows 下:

$df_c = disk_free_space("C:");

$df_d = disk_free_space("D:");

?>

注釋Note:此函數(shù)不能作用于遠(yuǎn)程文件,被檢查的文件必須是可通過服務(wù)器的文件系統(tǒng)訪問的。

參見Transformation is possible WITHOUT using loops:

$bytes = disk_free_space(".");

$si_prefix = array( 'B', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB');

$base = 1024;

$class = min((int)log($bytes , $base) , count($si_prefix) - 1);

echo $bytes . '
';

echo sprintf('%1.2f', $bytes / pow($base,$class)) . ' '. $si_prefix[$class] . '
';

?>Nice, but please be aware of the prefixes.

SI specifies a lower case 'k' as 1'000 prefix.

It doesn't make sense to use an upper case 'K' as binary prefix,

while the decimal Mega (M and following) prefixes in SI are uppercase.

Furthermore, there are REAL binary prefixes since a few years.

Do it the (newest and recommended) "IEC" way:

KB's are calculated decimal; power of 10 (1000 bytes each)

KiB's are calculated binary; power of 2 (1024 bytes each).

The same goes for MB, MiB and so on...

Feel free to read:

http://en.wikipedia.org/wiki/Binary_prefix$si_prefix = array( 'B', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB' );

you are missing the petabyte after terabyte

'B', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB'

should look like

'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'Another easy way to convert bytes to human readable sizes would be this:

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.Note that disk_free_space() does an open_basedir check.With respect to Linux filesystems, I'll point out that this function returns the space available in the current volume or mountpoint, not the total physical disk space. That is, this function used on the '/root' volume shows the free space in /root, which is different from '/home', and so on.<?php

function size($size, array $options=null) {

$o = [

'binary'=> false,

'decimalPlaces'=> 2,

'decimalSeparator'=> '.',

'thausandsSeparator'=> '',

'maxThreshold'=> false, // or thresholds key

'sufix'=> [

'thresholds'=> ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'],

'decimal'=> ' {threshold}B',

'binary'=> ' {threshold}iB'

]

];

if ($options !== null)

$o = array_replace_recursive($o, $options);

$count = count($o['sufix']['thresholds']);

$pow = $o['binary'] ? 1024 : 1000;

for ($i = 0; $i < $count; $i++)

if (($size < pow($pow, $i + 1)) ||

($i === $o['maxThreshold']) ||

($i === ($count - 1))

)

return

number_format(

$size / pow($pow, $i),

$o['decimalPlaces'],

$o['decimalSeparator'],

$o['thausandsSeparator']

) .

str_replace(

'{threshold}',

$o['sufix']['thresholds'][$i],

$o['sufix'][$o['binary'] ? 'binary': 'decimal']

);

}

var_dump(size(disk_free_space('/')));

// string(8) "14.63 GB"

var_dump(size(disk_free_space('/'), ['binary'=> true]));

// string(9) "13.63 GiB"

var_dump(size(disk_free_space('/'), ['maxThreshold'=> 2]));

// string(11) "14631.90 MB"

var_dump(size(disk_free_space('/'), ['binary'=> true, 'maxThreshold'=> 2]));

// string(12) "13954.07 MiB"

?>On Windows, this also works with distant files, by using their full network path.

For instance, this will give the % of free disk space on the share "dir" from remote host "server" :

$path = "\\\\server\\dir";

echo(floor(100 * disk_free_space($disk) / disk_total_space($disk)));

?>

It can also work with drive letters mapped to a network path in certain cases.

總結(jié)

以上是生活随笔為你收集整理的space index.php 7-14,disk_free_space()的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。