What are the potential pitfalls of using regular expressions to filter uploaded files in PHP?

Using regular expressions to filter uploaded files in PHP can be risky because it may not provide sufficient security against malicious file uploads. It is possible for attackers to bypass the regular expression filter and upload harmful files to the server. To enhance security, it is recommended to use functions like `pathinfo()` to extract the file extension and compare it against a whitelist of allowed extensions.

// Get the file extension using pathinfo()
$filename = $_FILES['file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);

// Whitelist of allowed file extensions
$allowed_extensions = array('jpg', 'jpeg', 'png', 'gif');

// Check if the file extension is in the whitelist
if (!in_array($ext, $allowed_extensions)) {
    // File extension not allowed, handle the error
    echo "Only JPG, JPEG, PNG, and GIF files are allowed.";
} else {
    // File extension allowed, proceed with file upload
    move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $filename);
}