Get CSV List of All Files on Your Domain
The software that comes with this article can create a CSV document of the file name, date, and size of every file in the document root and subdirectories of your domain.
Only the files in the document root and its subdirectories (the document area) will be listed. MySQL files are not in the document area, for example. Neither are cPanel statistics, as another example.
The document area is wherever web pages can be put for display in browsers. Usually, that is the directory where the domain's home or index page file is at, and all of its subdirectories.
A CSV file generally is easier to peruse than navigating a large site looking for certain files. The CSV has all your files listed in one document.
You might be surprised by the number of files in your domain and, perhaps, also by some of the file names.
When you have the CSV file, it can be imported into your spreadsheet software. Use it to see which are the largest files. Spot unnecessary files. There may be some totally unexpected files.
Sort the spreadsheet however you want to better assimilate what your document area contains.
-
Total the size column for the total size of all files in your document area.
-
Sort by file size for a view of the largest files. And perhaps find empty ones.
-
Sort by file date for date-range views.
-
Sort by file names to spot files that might not belong in the document area (or anywhere on your server).
-
Sort by file location for a by-directory view.
The Get File List CSV software was developed with the private server on my Mac which, a surprise to me, contained 1,387,170 files (less, now, after I deleted a bunch). After development, it was tested on 6 of our public domains. All were good, although I did tweak a script that was making more log files than I now needed.
Below is the software source code. Information about one optional customization follows.
<?php
/*
Get File List CSV
Version 1.0
January 15, 2022
Will Bontrager Software LLC
https://www.willmaster.com/
*/
/* Optional customization */
// Specify the CSV output file name.
$outputfilename = 'FileListOutput_'.date('Y-m-d_H-i-s').'.csv';
/* End of optional customization */
mb_regex_encoding('UTF-8');
mb_internal_encoding('UTF-8');
if( ! ini_get('date.timezone') ) { date_default_timezone_set('UTC'); }
set_time_limit(5*60*60);
ini_set('display_errors',1);
error_reporting(E_ALL);
$DocRoot = preg_quote($_SERVER['DOCUMENT_ROOT'],'/');
$OutputFile = fopen($outputfilename,'w');
if( $OutputFile )
{
fputs( $OutputFile, '"TimeStamp","FileSize","FileName","FileLocation"'."\n" );
$files = GetDirectoryFiles(__DIR__);
fclose($OutputFile);
echo 'OK';
}
else { echo 'Unable to open file to write'; }
exit;
function GetDirectoryFiles($dir)
{
global $OutputFile, $DocRoot;
$dir = preg_replace('!/*$!','',$dir);
$root = scandir($dir);
$result = array();
foreach($root as $value)
{
if($value === '.' || $value === '..') {continue;}
$flocation = trim("$dir/$value");
if(is_dir($flocation))
{
foreach(GetDirectoryFiles($flocation) as $value) { $result[]=$value; }
}
elseif(is_file($flocation))
{
$ta=stat("$dir/$value");
$linechunks = array();
$linechunks[] = date('Y-m-d H:i:s',$ta[9]);
$linechunks[] = $ta[7];
$linechunks[] = str_replace('"','""',$value);
$linechunks[] = str_replace('"','""',preg_replace('/^'.$DocRoot.'/','',$flocation));
fputs( $OutputFile, '"' . implode( '","', $linechunks ) . "\"\n" );
}
}
return $result;
}
?>
Optional customization —
The name/location for the CSV file may be changed.
Currently, the file is created in the directory where the software is running and the file name begins with FileListOutput_
, then the date and time, and ends with a .csv
file extension.
To change the name/location of the CSV file, change the blue text in the software source code to the name/location you prefer. For reference, here is a copy of the line with the blue text:
$outputfilename = 'FileListOutput_'.date('Y-m-d_H-i-s').'.csv';
This PHP script can be installed anywhere in the document area that allows PHP scripts to run.
Where you upload and run the script affects its output.
-
If the script is installed in the document root, the CSV file will list the files in the document area (the document root and all its subdomains).
-
However, if the script is installed in a subdomain of the document root, the CSV file will list only the files of that subdomain and its subdomains.
That feature allows you to obtain lists of files restricted to certain subdomains if that is what you prefer.
The PHP Get File List CSV script is fast. Depending on your server and how busy it is at the moment, the script shouldn't take more than a few seconds to get you a list of all files in the document area.
This article first appeared with an issue of the Possibilities newsletter.
Will Bontrager