ZipArchive addFromString doesn't work properly on Windows

810 Views Asked by At

I have a server whch runs a little app that we use to create .docx.

It worked perfectly on an old server but I had to move it on another one and now it has a great problem: when I run my php script - based on PHPWord - the output files are broken and cannot be used (MS Word reports "The file xyz.docx cannot be opened because there are problems with the contents).

Also changing the file extension from .docx to .zip (just to take a look to its content) doesn't help.

The "funny" side of this problem is that on the same server, using the same version of PHP, with nginx 1.2.3 it disappears and the .docx file are perfect (ok, if don't care of the content... but this is another problem...).

I could switch on nginx but I have a different issue on it: I can't read Latin1 special chars as expected.

After having googled and searched into the code for a long while, I found that the problem is with the following line, contained in the Template.php library of PHPWord, lin 105: $this->_objZip->addFromString('word/document.xml', $this->_documentXML);

It apparently works fine (and on nginx it really works fine) but it produces a fake .docx, impossible to open.

I tried different versions of the apache server with no luck, so I would really appreciate any help.

Summarizing:

  • I tested my script (which uses PHPWord) on a Windows Server 2003 host and php 5.2.8
  • It works fine with nginx
  • It produces a bad .docx file with apache (ZipArchive addFromString doesn't work)
  • I tested it using several Apache versions, from 2.0.64 up to 2.2.25
  • I can't see any error message

Any idea?

1

There are 1 best solutions below

0
On

You can use PclZip instead of ZipArchive.

The sample code :

\PhpOffice\PhpWord\Settings::setZipClass(\PhpOffice\PhpWord\Settings::PCLZIP);