PHP教學-偵測系統硬碟空間資訊
我們可以利用 PHP 所提供的函式來抓取主機系統的 cpu 、記憶體、硬碟空間等相關資訊,做成 web 系統時更可以讓系統管理人員隨時掌握住主機的各項硬體資訊。
範例程式如下:
<?php
//echo "目前可用 C 硬碟空間".disk_free_space("C:")."</br>";
// On Windows:
//echo "C 硬碟總容量".disk_total_space("C:")."</br>";
//$ds = disk_total_space("D:");
//This is a more readable way of viewing the returned float
// $Bytes contains the total number of bytes on "/"
$Bytes = disk_total_space("C:");
function dataSize($Bytes)
{
$Type=array("", "KB", "MB", "GB", "TB");
$counter=0;
while($Bytes>=1024)
{
$Bytes/=1024;
$counter++;
}
return("".$Bytes." ".$Type[$counter]."bytes");
}
echo "C 硬碟總容量".dataSize($Bytes). '<br />';
$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 . '<br />';
echo "目前可用 C 硬碟空間".sprintf('%1.2f' , $bytes / pow($base,$class)) . ' ' . $si_prefix[$class] . '<br />';
範例程式如下:
<?php
//echo "目前可用 C 硬碟空間".disk_free_space("C:")."</br>";
// On Windows:
//echo "C 硬碟總容量".disk_total_space("C:")."</br>";
//$ds = disk_total_space("D:");
//This is a more readable way of viewing the returned float
// $Bytes contains the total number of bytes on "/"
$Bytes = disk_total_space("C:");
function dataSize($Bytes)
{
$Type=array("", "KB", "MB", "GB", "TB");
$counter=0;
while($Bytes>=1024)
{
$Bytes/=1024;
$counter++;
}
return("".$Bytes." ".$Type[$counter]."bytes");
}
echo "C 硬碟總容量".dataSize($Bytes). '<br />';
$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 . '<br />';
echo "目前可用 C 硬碟空間".sprintf('%1.2f' , $bytes / pow($base,$class)) . ' ' . $si_prefix[$class] . '<br />';
留言
張貼留言