24

Here is my code where I am trying to send a correct header depedning on a type of a document. I figured out the headers for pdf, doc and docx but I still need to know correct header for Excel and Powerpoint files.

Any help appreciated.

    $document = urldecode($_GET['document']);
    $extension = end(explode('.', $document));
    $mimeType = '';
    switch ($extension) {
        case 'pdf':
            $mimeType = 'pdf';
            break;
        case 'doc':
            $mimeType = 'msword';
            break;
        case 'docx':
            $mimeType = 'msword';
            break;
        case 'xls':
            $mimeType = '';
            break;
        case 'xlsx':
            $mimeType = '';
            break;
        case 'ppt':
            $mimeType = '';
            break;
        case 'pptx':
            $mimeType = '';
            break;
    }       
    header('Content-type: application/' . $mimeType);
pnuts
  • 58,317
  • 11
  • 87
  • 139
Richard Knop
  • 81,041
  • 149
  • 392
  • 552
  • fabrik your link serves 'Error 404! The page you requested does not exist or has moved.' please remove your link from here. – Kamlesh Dec 31 '19 at 14:24

4 Answers4

100

.xls

application/vnd.ms-excel

.xlsx

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

.ppt

application/vnd.ms-powerpoint

.pptx

application/vnd.openxmlformats-officedocument.presentationml.presentation

And one of those you have listed is wrong:

.docx

application/vnd.openxmlformats-officedocument.wordprocessingml.document

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
5

See http://www.w3schools.com/media/media_mimeref.asp .

xls is application/vnd.ms-excel, ppt is application/vnd.ms-powerpoint.

Maerlyn
  • 33,687
  • 18
  • 94
  • 85
3

Since most of the types requested are Microsoft Office related, this link has all of the office mime types (the best focused source I've found so far): http://filext.com/faq/office_mime_types.php

Code Jockey
  • 6,611
  • 6
  • 33
  • 45
0

You can simply echo $_FILES['input_tag_name']['type'] after upload a file to know the MIME type of that file.

Biswadeep Sarkar
  • 841
  • 9
  • 17