Search Files on the Server
A few days ago, I needed to find all files at a specific website that contained a certain term.
The term needed to be removed as quickly as possible. Therefore, the search needed to be done quickly. And it needed to be accurate.
Below, you'll find the source code for a PHP script made to do that specific job.
The script has no dashboard. You need to specify the search term within the script. Then upload the script to the directory to be searched.
The PHP script will search files within the directory it is installed in. And search files within subdirectories. You can specify certain file name extensions for files that you do not want searched.
Here is the source code. Customization notes follow.
<?php /* Search for Phrase In Current Directory and Subdirectories Version 1.0 July 11, 2024 Will Bontrager Software LLC https://www.willmaster.com/ */ /* Customizations */ // Specify phrase to search for. $Phrase = 'Mr. Franklin'; // Provide list of file name extensions to omit from search. $OmitFileExtensions = 'pdf jpg jpeg gif png'; /* End of Customizations */ $Global = array(); $Global['Phrase'] = preg_quote($Phrase,'/'); $Global['Matches']=array(); $Global['Scanned'] = 0; $ta = array(); foreach( preg_split('/[\.\,\s]+/',trim($OmitFileExtensions)) as $item ) { $ta[] = '\.' . preg_quote($item,'/'); } $ta[] = preg_quote( preg_replace('!^.*/!','',$_SERVER['PHP_SELF']), '/' ); $Global['OmitList'] = implode('|',$ta); $scriptStart = time(); SearchList( __DIR__); $scriptStop = time(); $scriptElapsed = $scriptStop - $scriptStart; if(!$scriptElapsed) { $scriptElapsed = 1; } echo "<pre>Runtime seconds: $scriptElapsed\nCount of files scanned: {$Global['Scanned']}\nCount of files that contain a match: ".count($Global['Matches'])."\n"; foreach($Global['Matches'] as $file) { echo "$file\n"; } exit; function SearchList($dir) { global $Global; foreach(glob($dir.DIRECTORY_SEPARATOR.'*') as $value) { if(preg_match('/^\.\.?$/',$value)) { continue; } if(is_dir($value)) { SearchList($value); } elseif(is_file($value)) { if(preg_match('/'.$Global['OmitList'].'$/',$value)) { continue; } if(preg_match('/'.$Global['Phrase'].'/is',file_get_contents($value))) { $Global['Matches'][]=$value; } $Global['Scanned']++; } } } ?>
Customization —
-
Specify the word or phrase to search for.
At
$Phrase = 'Mr. Franklin';
replaceMr. Franklin
with your search term. -
Update the list of file name extensions to omit.
At
$OmitFileExtensions = 'pdf jpg jpeg gif png';
add or remove file name extensions from the purple-colored list. What you specify here will cause file names with those extensions to be skipped over while searching.
Installation —
Save the PHP script file as findphrase.php
or other *.php
file name that you prefer.
Upload the PHP script into the directory with files to be searched. Make a note of its URL.
Using —
To use, type the URL of the PHP script into your browser's address bar. The script will do the search and respond with this information:
- The number of seconds the script took to do the search.
- The number of files scanned.
- The number of files that contain a match.
- A list of the files where a match was found.
This is a handy tool for those instances when you need to find out the file name of every occurrence of a certain word or phrase.
(This content first appeared in Possibilities newsletter.)
Will Bontrager