Usually after completing a long project I find that I’ve created various extraneous files in the project directory; like zip files or maybe a few big graphic files or some huge MySQL dumps. If the directory sizes are small I can manually delete those unwanted files. But if the directory sizes are big or if they are nested deeply, than it can be quite time consuming. Maybe you left a couple of huge MySQL dumps somewhere and forgot to delete them, thus increasing the project file size. And if you are trying to do the cleanup on a online server then it can be even more painful.
Or maybe you are just curious to find how various types of files are taking up your directory space.
Whatever the reason, below is a small php script that displays the distribution of files in a particular directory and its sub-directories by its type. This can be handy if you would like to see which files are taking up space in your project. Although you can easily do such kind of things with a variety of desktop tools, the following code can easily be used online, or integrated into your existing php application.
Downloading the class
Before proceeding download the class from below.
Downloads : [downloadcounter(filestats)] / File size : [downloadsize(filestats)]
Displaying file stats
The class allows you to display the stats in a text format on the command-line or in a HTML format, which you can use online.
To display a text based stats you can use the class as given below and run it from the command line.
include('FileTypeStats.php'); $stats = new File_Type_Stats; $stats->getText("/localhost/home/project"); ?>
Below is the output for the above. Note that files that do not have an extension are not displayed.
D:\localhost\test\scan_dir>php textStat.php ===================================================== File Type Total Files Total Size % ===================================================== php 214 2149 kb (53.72 %) js 55 1183 kb (29.57 %) db 6 264 kb (6.60 %) css 20 144 kb (3.62 %) gif 90 124 kb (3.11 %) pdf 2 55 kb (1.40 %) jpg 48 45 kb (1.14 %) png 41 24 kb (0.62 %) log 3 5 kb (0.13 %) sql 1 3 kb (0.09 %) prefs 1 0 kb (0.00 %) txt 2 0 kb (0.00 %)
Setting a path in the function can be a bit limiting, so you can take the path argument from the command line instead.
include('FileTypeStats.php'); $stats = new File_Type_Stats; $stat_data = $stats->getText($argv); ?>
Now you can get the path from the command line.
D:\localhost\test\scan_dir>php textStat.php /localhost/home/project/
Set the textStat.php in you path variable, and you will have the file stat functionality available anywhere.
Someone could have written this class as a shell script easily, but the advantage of doing it in PHP is that we can also have a HTML version of the above stat display, as shown below.
To display HTML table based stats you can use the class with the getHtml() method and run it from the browser.
include('FileTypeStats.php'); $stats = new File_Type_Stats; $stat_data = $stats->getHtml("/localhost/home/project"); echo $stat_data; ?>
Below is the HTML output.
The style is defined in a CSS, so you can customize it to your liking.
A word of caution. For hugely nested directory running into thousands of files the script can timeout, so make sure that you increase the php max_execution_time in your script to an appropriate number.