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

WillMaster > LibrarySecurity and Blocking

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!

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 —

  1. Specify the word or phrase to search for.

    At $Phrase = 'Mr. Franklin';
    replace Mr. Franklin with your search term.

  2. 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

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