How to Find File and Directory Sizes in PHP

Topics focus on Web Developments, Programming, Database, Digital Marketing Tutorials, Code Snippets, SEO, Social Media Marketing and Inspirational articles.

How to Find File and Directory Sizes in PHP

How to Find File and Directory Sizes in PHP

How to Find File and Directory Sizes in PHP

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.

You can download this script as a .txt file. Remember to rename the file as a .php file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<!--?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&gt;=1024);    $size = round($size/1024)." KB"; break;
    default:               $size = $size." bytes"; break;
  }
  return $size;
}
// set size variables
if ($_POST['size'] &gt; 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.
";
// 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) &gt;= $max_size) {
          $print_size = sizeConv(filesize($path));
          $loop_result .="$file $print_size
";
        }
      } 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>
 
";
  $display_size = sizeConv($size);
  $message .="Disk space utilization = $display_size
";
  $message .= "Total file count = $count
";
  if (!empty($loop_result)) {
    $message .="
<b>List of files larger than $print_size:</b>
";
  } 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";
1
2
3
4
5
6
7
8
9
10
11
12
13
14
}
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
?&gt;
<form action="&lt;?php echo $_SERVER['SCRIPT_NAME']; ?&gt;" method="post" name="inputform">For a specific directory and all subdirectories, determine:
1) total space used
2) number of files
3) all files larger than a certain size
 
Directory <input name="dir" size="50" type="text" value="&lt;?php echo $path; ?&gt;" />
Size (in KB) <input name="size" size="6" type="text" value="&lt;?php echo $size; ?&gt;" />
<input name="submit" type="submit" value="Submit" />
</form>

Add comment


 

Pin It on Pinterest

Share This