How can the PHP function getimagesize() be used to determine the file type and set the file extension accordingly?
To determine the file type and set the file extension accordingly using the PHP function getimagesize(), you can retrieve the MIME type of the image using the 'mime' key in the returned array. Based on the MIME type, you can then set the appropriate file extension for the image file.
$image_path = 'path/to/image.jpg';
$image_info = getimagesize($image_path);
$mime_type = $image_info['mime'];
switch ($mime_type) {
case 'image/jpeg':
$file_extension = '.jpg';
break;
case 'image/png':
$file_extension = '.png';
break;
case 'image/gif':
$file_extension = '.gif';
break;
default:
$file_extension = '.jpg'; // default to jpg if MIME type is unknown
}