How can the PHP function pathinfo be used to enhance the validation of file types in PHP?
When validating file types in PHP, it's important to not solely rely on the file extension as it can be easily manipulated. The PHP function pathinfo can be used to extract the file extension and then compare it against a list of allowed file types to enhance the validation process.
$allowedFileTypes = ['jpg', 'jpeg', 'png', 'gif'];
$filePath = 'path/to/file.jpg';
$fileInfo = pathinfo($filePath);
$fileExtension = strtolower($fileInfo['extension']);
if(in_array($fileExtension, $allowedFileTypes)) {
echo 'File type is allowed.';
} else {
echo 'Invalid file type.';
}
Keywords
Related Questions
- How can PHP developers handle the issue of missing classes like "html" when creating select dropdowns?
- What are the best practices for setting the charset and encoding when working with forms and writing data to CSV files in PHP?
- What are some common pitfalls to watch out for when using require and require_once in PHP?