Solved

PHP to read available disk space.

Posted on 2004-08-25
4
266 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:malikatwork
  • 3
4 Comments
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 80 total points
ID: 11892824
If you have version 4.1 or later, you can use disk_free_space().  Otherwise, you can use one of the exec methods and pass the appropriate command to the OS.
http://www.php.net/manual/en/function.disk-free-space.php
http://www.php.net/manual/en/ref.exec.php
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11892884
use the disk_free_space function

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";
?>
0
 
LVL 27

Assisted Solution

by:Diablo84
Diablo84 earned 20 total points
ID: 11892905
or similarily in Gb

<?php
$space = disk_free_space("C:");
$space = (($space/1024)/1024)/1024;
echo round($space,2)." Gb";
?>
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11893783
A simple function i just wrote which may be useful to someone in the future:

<?php
function drivestat($drive,$graph=false) {

 $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/1024),1)." Gb";
 $free = round(($free/1024/1024/1024),1)." Gb";
 $used = round(($used/1024/1024/1024),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);
?>
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Exporting image to excel from php document 10 28
Loop through multiple arrays 13 32
How would I do this...? 2 28
How to obtain the string from a PHP StdObject ? 6 22
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question