What are the potential pitfalls of using wildcards in file paths in PHP?

Using wildcards in file paths can potentially lead to security vulnerabilities such as directory traversal attacks if not properly sanitized. To mitigate this risk, it is important to validate and sanitize user input before using it in file paths. One way to do this is by using the realpath() function to resolve the full path of a given filename, which helps to prevent directory traversal attacks.

// Sanitize user input before using it in file paths
$userInput = $_GET['file'];
$filePath = realpath('uploads/' . $userInput);

if ($filePath !== false && strpos($filePath, 'uploads/') === 0) {
    // File path is valid and safe to use
    // Proceed with file operations
} else {
    // Invalid file path, handle error accordingly
}