How can PHP developers properly check the MIME type of uploaded files on a server?
To properly check the MIME type of uploaded files on a server, PHP developers can use the `$_FILES` superglobal array to access the file information, including the MIME type. They can then compare the MIME type against a list of allowed MIME types to ensure the uploaded file is of the correct type.
$allowedMimeTypes = ['image/jpeg', 'image/png', 'image/gif'];
$uploadedFileMimeType = $_FILES['file']['type'];
if (in_array($uploadedFileMimeType, $allowedMimeTypes)) {
// File is of an allowed MIME type, proceed with processing
} else {
// File is not of an allowed MIME type, handle the error accordingly
}
Keywords
Related Questions
- What are some best practices for handling template variables in Smarty to avoid write file errors?
- Are there any best practices or guidelines to follow when setting the "bounce" in an email header in PHP?
- In the context of PHP programming, what are some best practices for efficiently replacing strings in a given text or data set?