malikatwork
asked on
PHP to read available disk space.
I have an image management system written in PHP. I need to display the amount of space available
on the hard drive for the system.
How can I use php to get available disk space. Can some one please provide sample code.
on the hard drive for the system.
How can I use php to get available disk space. Can some one please provide sample code.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
A simple function i just wrote which may be useful to someone in the future:
<?php
function drivestat($drive,$graph=fa lse) {
$total = disk_total_space($drive);
$free = disk_free_space($drive);
$used = $total - $free;
$percent = ($used/$total)*100;
$percent = round($percent,0);
$total = round(($total/1024/1024/10 24),1)." Gb";
$free = round(($free/1024/1024/102 4),1)." Gb";
$used = round(($used/1024/1024/102 4),1)." Gb";
$output =
"<b>Drive $drive Information:</b><br>\n
Total Capacity: $total<br>\n
Used: $used - Free: $free<br>\n
";
if ($graph == true) {
$output .= '
<table style="width: 180px; border: 1px solid #000000; font-size: 12px; color: #FFFFFF;" cellspacing="0">
<tr>
<td style="width: '.$percent.'%; background-color: #0000FF; text-align: center;">'.$percent.'%</td >
<td style="width: '.(100-$percent).'%; background-color: #FF0000; text-align: center;">'.(100-$percent). '%</td>
</tr>
</table>';
}
return $output;
}
echo drivestat("C:",true);
?>
<?php
function drivestat($drive,$graph=fa
$total = disk_total_space($drive);
$free = disk_free_space($drive);
$used = $total - $free;
$percent = ($used/$total)*100;
$percent = round($percent,0);
$total = round(($total/1024/1024/10
$free = round(($free/1024/1024/102
$used = round(($used/1024/1024/102
$output =
"<b>Drive $drive Information:</b><br>\n
Total Capacity: $total<br>\n
Used: $used - Free: $free<br>\n
";
if ($graph == true) {
$output .= '
<table style="width: 180px; border: 1px solid #000000; font-size: 12px; color: #FFFFFF;" cellspacing="0">
<tr>
<td style="width: '.$percent.'%; background-color: #0000FF; text-align: center;">'.$percent.'%</td
<td style="width: '.(100-$percent).'%; background-color: #FF0000; text-align: center;">'.(100-$percent).
</tr>
</table>';
}
return $output;
}
echo drivestat("C:",true);
?>
http://www.php.net/manual/en/function.disk-free-space.php
this will return it in bytes so if you want it in megabytes do this (for example)
<?php
$space = disk_free_space("C:");
$space = ($space/1024)/1024;
echo round($space,2)." Mb";
?>