PHP Check if a File is an Office 2007 Document by MIME Content Type

Posted on November 9, 2017 at 12:32 pm

Simple PHP function to check if a file is an Office 2007 document by filtering the content type:

function isOfficeDocumentByMIMEContentType($str)
{
	//https://blogs.msdn.microsoft.com/vsofficedeveloper/2008/05/08/office-2007-file-format-mime-types-for-http-content-streaming-2/
 
	$array = array(
	"application/msword", //doc
	"application/msword", //dot
	"application/vnd.openxmlformats-officedocument.wordprocessingml.document", //docx
	"application/vnd.openxmlformats-officedocument.wordprocessingml.template", //dotx
	"application/vnd.ms-word.document.macroEnabled.12", //docm
	"application/vnd.ms-word.template.macroEnabled.12", //dotm
	"application/vnd.ms-excel", //xls
	"application/vnd.ms-excel", //xlt
	"application/vnd.ms-excel", //xla
	"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", //xlsx
	"application/vnd.openxmlformats-officedocument.spreadsheetml.template", //xltx
	"application/vnd.ms-excel.sheet.macroEnabled.12", //xlsm
	"application/vnd.ms-excel.template.macroEnabled.12", //xltm
	"application/vnd.ms-excel.addin.macroEnabled.12", //xlam
	"application/vnd.ms-excel.sheet.binary.macroEnabled.12", //xlsb
	"application/vnd.ms-powerpoint", //ppt
	"application/vnd.ms-powerpoint", //pot
	"application/vnd.ms-powerpoint", //pps
	"application/vnd.ms-powerpoint", //ppa
	"application/vnd.openxmlformats-officedocument.presentationml.presentation", //pptx
	"application/vnd.openxmlformats-officedocument.presentationml.template", //potx
	"application/vnd.openxmlformats-officedocument.presentationml.slideshow", //ppsx
	"application/vnd.ms-powerpoint.addin.macroEnabled.12", //ppam
	"application/vnd.ms-powerpoint.presentation.macroEnabled.12", //pptm
	"application/vnd.ms-powerpoint.presentation.macroEnabled.12", //potm
	);
 
	foreach($array as $pattern)
	{
		if(stripos($str, $pattern) !== false)
		{
			return true;
		}
	}
 
	return false;
}

Useful links and resources:

Office 2007 File Format MIME Types for HTTP Content Streaming

Other Posts

Updated Posts