How can PHP developers ensure that only specific file formats are allowed for upload, such as jpg, gif, png, bmp, tga, and ico?
To ensure that only specific file formats are allowed for upload, PHP developers can use the `$_FILES` superglobal array to check the file type before moving the uploaded file to the server. This can be done by using the `pathinfo()` function to get the file extension and then comparing it with an array of allowed file formats.
$allowed_formats = array('jpg', 'gif', 'png', 'bmp', 'tga', 'ico');
$file_extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if (!in_array($file_extension, $allowed_formats)) {
echo "Only JPG, GIF, PNG, BMP, TGA, and ICO file formats are allowed.";
exit;
}
// Move the uploaded file to the server
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
echo "File uploaded successfully.";