What is a common method to determine if a file is an XML file in PHP?

One common method to determine if a file is an XML file in PHP is to check the file's content type by reading the first few bytes of the file and looking for the XML declaration "<?xml". If the file contains this declaration at the beginning, it is likely an XML file.

function isXMLFile($file) {
    $handle = fopen($file, &#039;r&#039;);
    $firstBytes = fread($handle, 5); // Read the first 5 bytes of the file
    fclose($handle);
    
    if (strpos($firstBytes, &#039;&lt;?xml&#039;) !== false) {
        return true;
    } else {
        return false;
    }
}

// Usage
$file = &#039;example.xml&#039;;
if (isXMLFile($file)) {
    echo &#039;The file is an XML file.&#039;;
} else {
    echo &#039;The file is not an XML file.&#039;;
}