How can PHP code be adjusted to restrict the file types allowed for upload to only images like jpg, gif, and bmp?
To restrict the file types allowed for upload to only images like jpg, gif, and bmp, you can modify the PHP code that handles file uploads to check the file type before allowing the upload to proceed. This can be done by checking the file extension or using functions like `getimagesize()` to verify the file type. If the uploaded file does not match the allowed image types, you can reject the upload.
$allowedTypes = ['image/jpeg', 'image/gif', 'image/bmp'];
if (in_array($_FILES['file']['type'], $allowedTypes)) {
// Process the file upload
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
echo 'File uploaded successfully.';
} else {
echo 'Only JPG, GIF, and BMP files are allowed for upload.';
}
Related Questions
- What are some best practices for error handling in PHP scripts, especially when establishing database connections or executing queries?
- How can SQL injection vulnerabilities be addressed in the provided PHP script?
- What are the potential pitfalls of using array functions like array_sum and array_filter in PHP, and how can they be avoided?