To limit file uploads to specific types, implement a conditional validation mechanism using the $_FILES global array and the in_array() function in PHP.
The code structure includes:
$allowed = array('image/jpeg', 'image/gif', 'application/pdf');
Here's the modified code:
$mime = $_FILES['foreign_character_upload']['type']; // File mime type $allowed = array("image/jpeg", "image/gif", "application/pdf"); if(!in_array($mime, $allowed)) { $error_message = 'Only jpg, gif, and pdf files are allowed.'; $error = 'yes'; }
If the file's mime type does not match any of the allowed types, the if-condition will be true and the $error and $error_message variables will be set accordingly. This enables you to handle the upload error and provide feedback to the user.
The above is the detailed content of How to Validate File Upload Types in PHP?. For more information, please follow other related articles on the PHP Chinese website!