How can PHP be used to list files that contain specific keywords in their content?

To list files that contain specific keywords in their content using PHP, you can iterate through each file in a directory, read its content, and check if the keyword exists in the content. If the keyword is found, you can store the file name in an array or display it directly.

<?php
// Directory to search for files
$directory = 'path/to/directory';

// Keyword to search for
$keyword = 'specific_keyword';

// Open the directory
$files = scandir($directory);

// Iterate through each file
foreach ($files as $file) {
    if (is_file($directory . '/' . $file)) {
        $content = file_get_contents($directory . '/' . $file);
        if (strpos($content, $keyword) !== false) {
            echo $file . PHP_EOL;
        }
    }
}
?>