How can one efficiently search for a file in the current directory and then move up a directory if not found in PHP?

To efficiently search for a file in the current directory and then move up a directory if not found in PHP, you can use the `file_exists()` function to check if the file exists in the current directory. If the file is not found, you can use `dirname()` to get the parent directory and then check if the file exists in that directory. This process can be repeated until the file is found or the root directory is reached.

$file = 'example.txt';
$currentDir = './';

while (!file_exists($currentDir . $file)) {
    $parentDir = dirname($currentDir);
    
    if ($parentDir === $currentDir) {
        echo 'File not found';
        break;
    }
    
    $currentDir = $parentDir;
}

echo 'File found in directory: ' . $currentDir;