What considerations should be taken into account when allowing custom file extensions for uploads in PHP to ensure proper validation?

When allowing custom file extensions for uploads in PHP, it is important to properly validate the file extensions to prevent security risks such as executing malicious code. One way to ensure proper validation is to compare the file extension against a list of allowed extensions and only allow uploads with those extensions.

// Define an array of allowed file extensions
$allowed_extensions = array('jpg', 'png', 'pdf');

// Get the uploaded file extension
$uploaded_extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

// Check if the uploaded file extension is in the list of allowed extensions
if (!in_array($uploaded_extension, $allowed_extensions)) {
    // File extension is not allowed, handle the error accordingly
    echo 'Invalid file extension. Please upload a file with a valid extension.';
    exit;
}

// Continue with the file upload process