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.";
}
Related Questions
- How can PHP be used to validate email addresses entered in a form?
- What potential security risks are associated with using the provided PHP script for file downloads?
- What steps can be taken to troubleshoot and fix PHP syntax errors like unexpected '<' in functions.php files, especially after a browser crash?