Recursively search string in all folders in cPanel – PHP

Suppose you are working on cPanel on a very big project, now you want to search some string in all folders and in all of their files in cPanel. There are very few cPanel that provide such functionality to search some specific string in all files in your domain. This script will allow you to do this.

You just needs to put this file in the root folder of your domain where you want to perform search. For the sake of simplicity we are creating a simple form where you will enter the string that needs to be searched and hit enter to do the search.

Creating a form to enter string

<form method="GET" action="search.php">
	<input name="search" placeholder="Enter query" autocomplete="off">

	<input type="submit" name="submit">
</form>

Perform search

Create a recursive function that will search for all files. While calling this function you need to send the root folder path in the first call. If you are using Laravel you need to use Laravel built-in base_path() instead of core PHP dirname(__FILE__). Create a new file named “search.php” and paste the following code in it:

search.php

<?php

	// A recursive function to search all files
	function get_directory_content($directory)
	{
		//
	}

	// Check if form submits
	if (isset($_GET["submit"]))
	{
		// Get searched query
		$search = $_GET["search"];

		// Where all files (where that string is found) will be stored
		$results = array();

		// Send root folder path to start the search
		get_directory_content(dirname(__FILE__));
	}

As we will be using $search and $results variable inside the function so we need to declare them as global variables, otherwise they will not be accessible inside the function. Also we need to get all the files inside the folder which is being passed as parameter, loop through all the files.

Skip “.” and “..”

In Unix file system there are 2 commands inside every folder which are “.” and “..”. The first one means the current directory and second one means the parent directory. In this case we will skip the loop by using continue; command.

global $search, $results;

$files = scandir($directory);
foreach ($files as $file)
{
	if ($file == "." || $file == "..")
	{
		continue;
	}
}

Next, we will check if the incoming name is a directory or a file. If it is a file then we will simply process it for searching text. If it is a directory then we will call the recursive function again to get its files too. In this way it will check for all files no matter how many nested folders you have.

$is_file = false;
$path = realpath($directory . DIRECTORY_SEPARATOR . $file);

if (is_dir($path))
{
	get_directory_content($path);
	$is_file = true;
}
else
{
	$is_file = true;
}

Then we will get the content of that file and check if that searched query exists in that file. stripos(content, search) will search case-insensitively which means that you do not have to worry about small and capital letters.

stdClass in PHP

If that text is found in file then we will create a custom object using built-in PHP stdClass() class. If you are working on Laravel then you need to push back-slash before the class name, for example for Laravel \stdClass() because Laravel classes works in namespaces.

Set the name of file in that object and lastly push that object in the $results array we created in second step.

if ($is_file)
{
	$content = file_get_contents($path);

	if (stripos($content, $search) !== false)
	{
		$obj = new stdClass();
		$obj->file_name = $file;

		array_push($results, $obj);
	}
}

Now we will know the name of file where your searched string is found. But we would also want to know the line number in case you have larger files. Also if you were searching for some variable then it will have multiple occurrences in same file so we need to create an array. Another variable will be created to tell the line number of each occurrence.

In order to check each line number we need to loop through each line of that file. To do that, first open the file in read mode using fopen(“file_name”, “r”) and this will return the file handler which we can use later.

fgets in PHP

fgets(file_handler, length) function will read one line each time it is called and by specifying length paramter it will move to the next line every time it is called. In each iteration, we are increasing the number of line by incrementing the variable with 1.

Then we will use the same function to search but this time for single line and not for whole file. If found then we will push that line number in $lines array.

After the while loop we will assign that lines array to our custom object we created in previous step. So lines array will also be sent along with file name which we did in previous step.

$lines = array();
$line_number = 0;

$file_handler = fopen($path, "r");
while ( ( $line = fgets($file_handler, 4096) ) !== false )
{
	$line_number++;

	if (stripos($line, $search) !== false)
	{
		array_push($lines, $line_number);
	}
}

$obj->lines = $lines;

The full recursive function is given below:

function get_directory_content($directory)
{
	global $search, $results;

	$files = scandir($directory);
	foreach ($files as $file)
	{
		if ($file == "." || $file == "..")
		{
			continue;
		}

		$is_file = false;
		$path = realpath($directory . DIRECTORY_SEPARATOR . $file);

		if (is_dir($path))
		{
			get_directory_content($path);
			$is_file = true;
		}
		else
		{
			$is_file = true;
		}

		if ($is_file)
		{
			$content = file_get_contents($path);

			if (stripos($content, $search) !== false)
			{
				// Laravel, \stdClass()
				$obj = new stdClass();
				$obj->file_name = $file;

				$lines = array();
				$line_number = 0;

				$file_handler = fopen($path, "r");
				while ( ( $line = fgets($file_handler, 4096) ) !== false )
				{
					$line_number++;

					if (stripos($line, $search) !== false)
					{
						array_push($lines, $line_number);
					}
				}

				$obj->lines = $lines;

				array_push($results, $obj);
			}
		}
	}
}

To display the data is quite simple, just call the function and loop through $results array.

<?php

get_directory_content(dirname(__FILE__));

foreach ($results as $result):
?>

	<h1><?php echo $result->file_name; ?></h1>

	<h2>Lines</h2>
	<ul>
		<?php foreach ($result->lines as $line) { ?>
		<li>
			<?php echo $line; ?>
		</li>
		<?php } ?>
	</ul>

<?php endforeach; ?>

Hope this tutorial helps you in search your query string in all folders in your cPanel. If it did not work, or if you are having any error in this, kindly do let us know.

Learn more

Search in all tables and columns – PHP & MySQL

[wpdm_package id=’191′]