Skip to content

Commit

Permalink
Merge pull request #2416 from gamerlv/bugfix/GH1753-finfo-mimetype
Browse files Browse the repository at this point in the history
Fix wrong mimetype for docx files
  • Loading branch information
Progi1984 authored Aug 12, 2023
2 parents 6590189 + 9b8a84c commit 409172c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/PhpWord/Writer/Word2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ public function __construct(?PhpWord $phpWord = null)
$this->setPhpWord($phpWord);

// Create parts
// The first four files need to be in this order for Mimetype detection to work
$this->parts = [
'ContentTypes' => '[Content_Types].xml',
'Rels' => '_rels/.rels',
'RelsDocument' => 'word/_rels/document.xml.rels',
'Document' => 'word/document.xml',
'DocPropsApp' => 'docProps/app.xml',
'DocPropsCore' => 'docProps/core.xml',
'DocPropsCustom' => 'docProps/custom.xml',
'RelsDocument' => 'word/_rels/document.xml.rels',
'Document' => 'word/document.xml',
'Comments' => 'word/comments.xml',
'Styles' => 'word/styles.xml',
'Numbering' => 'word/numbering.xml',
Expand Down
22 changes: 22 additions & 0 deletions tests/PhpWordTests/Writer/Word2007Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace PhpOffice\PhpWordTests\Writer;

use finfo;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\SimpleType\Jc;
use PhpOffice\PhpWord\Writer\Word2007;
Expand Down Expand Up @@ -192,4 +193,25 @@ public function testSetUseDiskCachingException(): void
$object = new Word2007();
$object->setUseDiskCaching(true, $dir);
}

/**
* File is detected as Word 2007.
*/
public function testMime(): void
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addText('Test 1');

$writer = new Word2007($phpWord);
$file = __DIR__ . '/../_files/temp.docx';
$writer->save($file);

$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($file);

self::assertEquals('application/vnd.openxmlformats-officedocument.wordprocessingml.document', $mime);

unlink($file);
}
}

0 comments on commit 409172c

Please sign in to comment.