This class can be used to monitor the size of files in a folder.
It can traverse a given directory recursively and count the size that files are taking on the disk.
The class can repeat the process after a given period of time and display a chart using the
Google Chart API to show the current disk space usage and change over time.
$fillfolder = new Supervisor_of_filling_of_folders();
$fillfolder ->supfillfolder("youfolder/",400,250,2,5368709120);// parameters $url, $width_img, $height_img, $interval,$max_value in bytes, example 5368709120 = 5 Gb
echo "<meta http-equiv='refresh' content='2' />"; // use same interval value, you can modifying and apply the AJAX technology
?>
Supervisor_of_filling_of_folders.php
<?php
/* Supervisor_of_filling_of_folders 0.2.0 by Roberto Aleman, ventics.com,BSD Licence
Hi traffic websites, may have folders holding UGC (User Generated content) such as images and documents
that are uploaded by the users to shared folders. If a quota exists for each user, it would be helpful to have
an alarm that shows an estimate of when it would be needed to intervene and increase the size of the Quota
or disk space.
Using the Google Chart API, you can build interesting and important controls that would relay a visual status
of what is happening on the server to the webmaster.
With a little work, the users of this class may apply to a control panel using AJAX.
This class sends several parameters:
$url to monitor: which should be on the same server running the script.
$width_img and $height_img : chart measures for Google Chart
$time: the time or interval monitoring.
$max_value: critical size reference for the calculations.
*/
class Supervisor_of_filling_of_folders
{
function supfillfolder($url, $width_img,$height_img,$time,$max_value)
{
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($url)) as $file){
$size1+=$file->getSize(); //interator to get real size of directory time 1
}
sleep($time);
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($url)) as $file){
$size2+=$file->getSize(); //interator to get real size of directory time 2
}
$variation = $size2 - $size1;
$growth = ($variation / $max_value) *100; //calculate growth between time 1 and time 2
$average = ($size2 - $size1)/$time; //calculate average bytes/seconds
echo "<img src='http://chart.apis.google.com/chart?chs=".$width_img."x".$height_img."&cht=gom&chd=t:".$growth. "&chco=FF0000,FF8040,FFFF00,00FF00,00FFFF,0000FF,800080&chxt=x,y&chxl=0:%7CGrowth%7C1:%7Cslow%7Cfaster%7Ccrazy' alt='Directory Supervisor'>"; //show google chart the growth
echo "<br/><img src='http://chart.apis.google.com/chart?chs=".$width_img."x".$height_img."&cht=gom&chd=t:".$average. "&chco=FF0000,FF8040,FFFF00,00FF00,00FFFF,0000FF,800080&chxt=x,y&chxl=0:%7CAverage%7C1:%7Cslow%7Cfaster%7Ccrazy' alt='Directory Supervisor'>"; //show google chart the average
echo "<br/>Disk Critical Limit : ".$max_value." bytes <br/>";
echo "Current growth : ".$growth." % of Max Value<br/>";
echo "Current Average : ".$average." bytes/seconds<br/>";