How can the PHP script be modified to display an error message for incorrect file formats?
To display an error message for incorrect file formats in a PHP script, you can check the file format before processing it and display an error message if it does not match the expected format. This can be done by using a conditional statement to compare the file format with the allowed formats and then displaying an error message if it does not match.
<?php
$allowedFormats = ['jpg', 'jpeg', 'png', 'gif'];
$uploadedFile = $_FILES['file']['name'];
$uploadedFileFormat = pathinfo($uploadedFile, PATHINFO_EXTENSION);
if (!in_array($uploadedFileFormat, $allowedFormats)) {
echo "Error: Incorrect file format. Please upload a file in JPG, JPEG, PNG, or GIF format.";
} else {
// Process the file if it has the correct format
}
?>
Keywords
Related Questions
- What are some common security risks associated with using pre-made form mailers in PHP?
- How can the Google Maps API be effectively integrated into a website while ensuring compliance with usage terms?
- How can configuration settings for Mercury Mail Server impact the functionality of the mail() function in PHP scripts?