Are there any potential pitfalls or security concerns to be aware of when using glob() in PHP for file searches?

One potential security concern when using glob() in PHP for file searches is the possibility of directory traversal attacks. To mitigate this risk, it is important to sanitize user input and ensure that the path provided to glob() is restricted to a specific directory or set of directories.

// Example of sanitizing user input before using glob()
$directory = '/path/to/files/';
$searchTerm = isset($_GET['search']) ? $_GET['search'] : '';

// Validate and sanitize user input
if (strpos($searchTerm, '..') !== false) {
    die('Invalid search term');
}

// Use glob() with the sanitized directory path
$files = glob($directory . '*' . $searchTerm . '*');