From 9b8a84c39c67443aa89b9b7759e9fd5bb8b89c44 Mon Sep 17 00:00:00 2001 From: Levi Voorintholt Date: Thu, 29 Jun 2023 15:33:06 +0200 Subject: [PATCH] Change order of files inside Word file to be compatible with common mimetype detection --- src/PhpWord/Writer/Word2007.php | 5 +++-- tests/PhpWordTests/Writer/Word2007Test.php | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/PhpWord/Writer/Word2007.php b/src/PhpWord/Writer/Word2007.php index 3895584671..9e17efa611 100644 --- a/src/PhpWord/Writer/Word2007.php +++ b/src/PhpWord/Writer/Word2007.php @@ -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', diff --git a/tests/PhpWordTests/Writer/Word2007Test.php b/tests/PhpWordTests/Writer/Word2007Test.php index 32b046f01d..94f9caf8a1 100644 --- a/tests/PhpWordTests/Writer/Word2007Test.php +++ b/tests/PhpWordTests/Writer/Word2007Test.php @@ -17,6 +17,7 @@ namespace PhpOffice\PhpWordTests\Writer; +use finfo; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\SimpleType\Jc; use PhpOffice\PhpWord\Writer\Word2007; @@ -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); + } }