<?php
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);

$search = "select2";
// $search = 'status!=0';
$dir = '/home/jobaaj/public_html/app.watify.io';
// $folder = ['oldpayments', 'old_sales', 'newsales2backup'];

function scanDirectory($dir, $search)
{
    $files = scandir($dir);
    foreach ($files as $file) {
        if ($file === '.' || $file === '..') {
            continue;
        }

        // if ($dir=='/home/nishtyainfotech/public_html/' && in_array($file, $GLOBALS['folder'])) {
        //     continue;
        // }

        $filePath = $dir . '/' . $file;
        if (is_dir($filePath)) {
            scanDirectory($filePath, $search); // Recursive call for inner directories
        } else {
            if (strlen($file) > 3 && in_array(pathinfo($file, PATHINFO_EXTENSION), ['php','js'])) {
                $readfile = fopen($filePath, 'r');

                while (!feof($readfile)) {
                    $contents = fgets($readfile);
                    if (stripos($contents, $search) !== false) {
                        // $fp = fopen("/home/nishtyainfotech/public_html/jobaaj.com/jobaaj_cdn.txt", 'a');
                        // fwrite($fp, "$filePath\r\n");
                        // fclose($fp);
                        echo $filePath . '<br>';
                        break; // Stop reading the file once the search query is found
                    }
                }

                fclose($readfile);
            }
        }
    }
}

scanDirectory($dir, $search);
// echo "<pre>";
// print_r($_SERVER);
// echo "</pre>";
