In the given code snippet, what is the purpose of the is_filename function and how does it validate the $dir variable?

The is_filename function is used to validate if a given filename is valid or not. In the code snippet provided, the is_filename function checks if the $dir variable contains only alphanumeric characters, underscores, and hyphens. If the $dir variable does not meet these criteria, the function returns false, indicating that the filename is invalid.

function is_filename($dir) {
    return preg_match('/^[a-zA-Z0-9_-]+$/', $dir);
}

$dir = "example_dir123";
if (is_filename($dir)) {
    echo "Valid filename.";
} else {
    echo "Invalid filename.";
}