Are there any potential security risks to consider when uploading images in PHP?

When uploading images in PHP, one potential security risk to consider is the possibility of allowing users to upload malicious files disguised as images. To mitigate this risk, it is important to validate the file type and ensure that only allowed image formats are accepted for upload.

// Check if the uploaded file is an image
$allowedFormats = ['jpg', 'jpeg', 'png', 'gif'];
$uploadedFile = $_FILES['image']['tmp_name'];
$detectedFormat = exif_imagetype($uploadedFile);

if ($detectedFormat === false || !in_array(image_type_to_extension($detectedFormat, false), $allowedFormats)) {
    // Invalid file format
    die('Invalid image format. Only JPG, JPEG, PNG, and GIF files are allowed.');
}

// Continue with image upload process