How to Find File and Directory Sizes using PHP

Post Reply
Tony
Lieutenant
Lieutenant
Posts: 86
Joined: Tue Jul 21, 2009 4:11 pm

How to Find File and Directory Sizes using PHP

Post by Tony » Sun Nov 29, 2009 4:41 am

This php script will search any directory (and all subdirectories under that directory). It will show you the total size of the directory, number of files in the directory, and identify any files > or = to the size specified. When you run this script you will be prompted for the unix directory name and the test file size parameter.

This script can be very helpful if you have multiple ftp users and you want to monitor file or directory sizes.

Code: Select all

<?php
// if (isset($_POST['submit']) AND is_dir($_POST['dir']) AND is_numeric($_POST['size'])) { 
if (isset($_POST['submit'])) { 
$dir = $_POST['dir'];
// size conversion function
function sizeConv($size) {
  switch ($size) {
    case ($size>=1048576): $size = round($size/1048576)." MB"; break;
    case ($size>=1024);    $size = round($size/1024)." KB"; break;
    default:               $size = $size." bytes"; break;
  }
  return $size;
}
// set size variables
if ($_POST['size'] > 0) {
  $max_size = $_POST['size'] * 1000;
  $print_size = sizeconv($max_size);
} else $message .= "<b>" . $_POST['size'] . "</b> is not a valid size parameter, value ignored.<br>";
// get info function
function getInfo($dir) { 
  global $size, $max_size, $loop_result, $count;
  foreach(scandir($dir) as $file) {
    if ($file != "." and $file != "..") { 
      $path = $dir."/".$file; 
      if (is_file($path)) {
        $size += filesize($path); 
        $count++;
        if (isset($max_size) AND filesize($path) >= $max_size) {
          $print_size = sizeConv(filesize($path));
          $loop_result .="$file $print_size<br>";
        }
      } else {
        if (is_dir($path)) getInfo($path);
      }
    }
  }
}
// processing mainline
if (file_exists($dir)) {
  $size = 0; $count = 0; unset($loop_result);
  getInfo($dir);
  $message .= "<b>Statistics for directory $dir</b><br><br>";
  $display_size = sizeConv($size);
  $message .="Disk space utilization = $display_size<br>";
  $message .= "Total file count = $count<br>";
  if (!empty($loop_result)) {
    $message .="<br><b>List of files larger than $print_size:</b><br>";
  } else {
    if (empty($print_size)) $print_size = "0 KB";
    $loop_result .="No files found larger than $print_size";
  }
  $message .= $loop_result;
} else {
  $message .= "Directory <b>$dir</b> does not exist, please re-enter a vaild directory path.";
}
// output
echo "$message<br><hr>";
}
if (empty($_POST['dir'])) $path ="Put your root or relative path here"; else $path = $_POST['dir'];
if (empty($_POST['size'])) $size ="200"; else $size = $_POST['size'];
// input form
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" name="inputform">
For a specific directory and all subdirectories, determine:
<br>1) total space used<br>2) number of files
<br>3) all files larger than a certain size<br>
<br>Directory <input type="text" value="<?php echo $path; ?>" name="dir" size="50">
<br>Size (in KB) <input type="text" value="<?php echo $size; ?>" name="size" size="6">
<br><input type="submit" name="submit" value="Submit">
</form>
Mysoogal
Captain
Captain
Posts: 223
Joined: Thu Dec 17, 2009 7:15 am
Location: Planet VPS

Re: How to Find File and Directory Sizes using PHP

Post by Mysoogal » Wed Dec 23, 2009 4:58 am

thanks for this, good thing to have since you have lots of folders and videos :D
Post Reply

Return to “PHP & MySQL”