0

I download some files, such as word, ppt, excel. But i don't know their MIMETYPE and suffix. Is there some way can get MIMETYPE of these file?

Rob
  • 415,655
  • 72
  • 787
  • 1,044
coderL
  • 73
  • 1
  • 7

1 Answers1

1

If you don't know the suffix, you're forced to look at the file contents. Typically this starts with looking for magic bytes, the first few bytes of the file. You can often qualify the type of file on that basis (though you obviously can't be sure unless you validate the whole file).

For modern Office documents, they should conform to OOXML and the first two bytes should be 0x50 0x4b (i.e. "PK") the indicator of a zip file.

You can then uncompress it (e.g. with ZipArchive).

You can then either parse the docProps/app.xml or see Office Open XML site with links at the top of the page for how to parse word processing, excel, and presentations, respectively.

Rob
  • 415,655
  • 72
  • 787
  • 1,044