Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Development and Maintenance

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Directory Content Size

For dynamic sites that log activity in the public document area, it is prudent to occasionally get a list of directories and the total size of the files they contain. Log files may quickly get so large they need to be removed.

The Directory Content Size software (source code below) can provide a list of directories and their content size.

Directory Content Size has another use. When moving a domain to a different server, a directory list obtained from the old server can be compared to a directory list obtained from the new server. If any on the old server list are absent on the new server list, you'll know which directories still need to be moved.

Directory Content Size is PHP software. Here is the source code:

<?php
/* 
   Directory Content Size
   Version 1.1
   January 18, 2014
   Version 1.0 - April 23, 2012
   Version 1.1 - January 18, 2014 - Added total file size.

   Will Bontrager Software LLC
   https://www.willmaster.com/
   Copyright 2012,2014 Will Bontrager Software LLC

   This software is provided "AS IS," without 
   any warranty of any kind, without even any 
   implied warranty such as merchantability 
   or fitness for a particular purpose.
   Will Bontrager Software LLC grants 
   you a royalty free license to use or 
   modify this software provided this 
   notice appears on all copies. 
*/
?><!DOCTYPE html>
<head>
<title>Directory Content Size</title>
<style type="text/css">
body { font-family: sans-serif; margin:50px 0 0 100px; background-color:white; }
.inputwide { width:500px; }
.inputfont { font-family:monospace; font-size:larger; }
#content { width:500px; position:relative; }
</style>
</head>
<body>
<div style="position:absolute; left:30px; top:100px;">
<a href="//www.willmaster.com/">
<img src="//www.willmaster.com/images/wmlogo_icon.gif" border="0" width="50" height="50" alt="Willmaster.com logo - Will Bontrager Software LLC" title="The best supported website software - Will Bontrager Software LLC">
</a>
</div>
<div id="content">
<h2 style="margin-top:0; color:#333;">Directory Content Size</h2>
<form method="post" action="<?php echo($_SERVER['PHP_SELF']); ?>">


<?php if( isset($_POST['doreporting']) ): ?>
<h3>Directories &ndash;</h3>
<p>
Listed below are the directories with the total file sizes of the regular files they contain.
</p>
<pre>
Size (bytes)  Directory
------------  ---------
<?php
$TotalSize = 0;
$directory = trim($_POST['directory']);
if( $directory != '/' ) { $directory = preg_replace( '!/*$!', '', $directory ); }
if( $directory == '' ) { $directory = preg_replace('!/[^/]*$!','',$_SERVER['PHP_SELF']); }
$directory = $_SERVER['DOCUMENT_ROOT'] . '/' . preg_replace('!^/*!','',$directory);
$directory = preg_replace('!/*$!','',$directory);
$subdirs = (isset($_POST['subdirs']) and $_POST['subdirs'] == 1) ? true : false;
$dirlist = $donedirlist = array();
$dirlist[] = $directory;
$dircount = 1;
while( count($dirlist) )
{
   $dir = array_shift($dirlist);
   $newdirs = glob("$dir/*");
   $dirsize = 0;
   foreach( $newdirs as $file )
   {
      if( is_dir($file) )
      {
         if( $subdirs )
         {
            $dirlist[] = $file;
            $dircount++;
         }
      }
      elseif( is_file($file) )
      {
         $size = filesize($file);
         $dirsize += $size;
         $TotalSize += $size;
      }
   }
   $dir4list = preg_replace('!^\Q' . $_SERVER['DOCUMENT_ROOT'] . '\E!', '', $dir);
   if( empty($dir4list) ) { $dir4list = '/'; }
   $donedirlist[ $dir4list ] = $dirsize;
}
ksort($donedirlist);
foreach( $donedirlist as $dir => $size )
{
   $sizestr = strval($size);
   while( strlen($sizestr) < 13 ) { $sizestr .= ' '; }
   echo "$sizestr $dir\n";
}
$s = "$TotalSize (TOTAL)";
$ts = '-';
while( strlen($ts) < strlen($s) ) { $ts .= '-'; }
echo "$ts\n$s\n";
$ts = str_replace('-','=',$ts);
echo $ts;
?>
</pre>
<p>
<?php echo($dircount); ?> director<?php echo( ($dircount==1?'y':'ies') ); ?> listed above.
</p>
</p>
&nbsp;<br>
<hr width="100">
<p style="margin-bottom:0;">
<a href="<?php echo($_SERVER['PHP_SELF']); ?>">[HOME]</a>
</p>


<?php else: ?>
<p>
This software will determine the total size of files within the directory you specify and, optionally, its subdirectories.
</p>
<p>
Specify the directory:
</p>
<ul>
<li>
<p>
To specify the current directory, the directory where this software is installed, leave this blank.
</p>
</li>
<li>
<p>
To specify the document root directory, specify only "/" (no quotes).
</p>
</li>
<li>
<p>
Otherwise, specify the directory relative to the document root.
</p>
</li>
</ul>
<p style="margin-bottom:3px;">
Directory:
</p>
<input type="text" class="inputwide inputfont" name="directory">
<p style="margin-top:5px;">
<input type="checkbox" style="margin:0;" name="subdirs" value="1"> 
And all subdirectories.
</p>
<div id="buttonp" style="display:none; border:1px solid black; padding:12px; font-size:14px; font-weight:bold; text-align:center;">This may take a while. ...</div>
<p style="margin-top:25px;">
<input type="submit" class="inputwide" name="doreporting" value="List Directories" onclick="PublishMessage()">
</p>
<script type="text/javascript">
function PublishMessage() {
var d = document.getElementById("buttonp");
d.style.display = "";
}
</script>
<?php endif; ?>


&nbsp;<br>
<hr width="100" align="left">
<p>
Copyright 2012 <a href="//www.willmaster.com/">Will Bontrager Software LLC</a>
</p>
</form>
</div>
</body>
</html>

Save the above as ServerDirectories.php or other appropriate name ending with .php

Upload the ServerDirectories.php file to the server. Then, type its URL into your browser's address bar.

You'll see a page with a text field to specify the directory to scan, scanning its subdirectories being optional.

  • To specify the current directory, the directory where ServerDirectories.php is installed, leave the field blank.

  • To specify the document root directory, specify only a slash character, / (The document root is the directory where the domain's main or index page is located.)

  • Otherwise, specify the directory relative to the document root. (That would be the URL to the directory with the http and domain name removed. Example: http://example.com/books/ is specified as /books/)

To scan the specified directory and also its subdirectories, check the "And all subdirectories" checkbox.

Click the "List Directories" button.

After the button is clicked, the Directory Content Size software will list each directory and its content size. Following the list is a count of number of directories that were scanned and the total of the content sizes.

That's all it takes. Clicking the button will scan the specified directory and, optionally, all its subdirectories. Then print the results.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC