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
- Are there any recommended online tools for testing and debugging regular expressions in PHP?
- How can the PHP max() function be correctly used to find the maximum value of a column in a MySQL query result?
- What are the potential pitfalls of saving PHP files with Unicode encoding, especially in relation to special characters and umlauts?