How can the validation process for file uploads in a PHP form mailer be improved to ensure only JPEG and PNG files are accepted?
To improve the validation process for file uploads in a PHP form mailer to ensure only JPEG and PNG files are accepted, you can check the file's MIME type using the `$_FILES` superglobal array and verify that it corresponds to either "image/jpeg" or "image/png". This will prevent any other file types from being uploaded.
// Check if file is a valid JPEG or PNG
if ($_FILES['file']['type'] != 'image/jpeg' && $_FILES['file']['type'] != 'image/png') {
echo 'Only JPEG and PNG files are allowed.';
exit;
}
// Process file upload
// Your existing code for handling file uploads goes here
Keywords
Related Questions
- Is it advisable to rely on PHP to extract and insert the title of a previous page into a URL redirect?
- What are the limitations of CSS in terms of dynamically changing font styles based on content?
- What best practices should be followed when processing checkbox data in PHP to prevent malicious attacks like Cross Site Scripting (XSS)?