views:

31

answers:

2

We're trying to send uploaded attachements (served from a database as blob) through php with Zend Framework to a client.

This code works fine for Excel97 / Word97.

  if ($this->getResponse()->canSendHeaders(false)) {
            $response = $this->getResponse();

            $response->setHeader('Pragma', 'public', true)
                     ->setHeader('Expires', '0', true)
                     ->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
                     ->setHeader('Content-Type', 'application/force-download', true)
                     ->setHeader('Content-Type', 'application/octet-stream', true)
                     ->setHeader('Content-Type', 'application/download', true)
                     ->setHeader('Content-Disposition', "attachment;filename=$filename", true)
                     ->setHeader('Content-Transfer-Encoding', 'binary', true)
                     ->setBody($data) // binary
                     ->sendHeaders();
        } 

But is not working for excel2007 / word2007. It reports "file has an error" and trys to fix it.

Any suggestions?

A: 

Different MIME types: http://filext.com/faq/office_mime_types.php, at least that solved the problem for me when I had a similar one

DrColossos
still the same error wit "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
ArneRie
+1  A: 

You should send the correct Content-Type for the given file format. That should be application/msword/application/vnd.ms-excel if you're sending old .doc- or .xls-files and application/vnd.openxmlformats-officedocument.wordprocessingml.document or application/vnd.openxmlformats-officedocument.spreadsheetml.sheet for the newer x-type files .docx and .xlsx respectively (don't know which version you're serving).

Further more, as far as I know, the newer Office programs check if the file extension matches the file content, so you'll get errors or warnings when opening .doc-files with an .docx-extension.

A ...->setHeader('Content-Type', '<<the appropriate content-type>>', true) should be sufficient.

Stefan Gehrig
crazy excel still reports broken file (after this message it is working)..
ArneRie
You're sure that the file doesn't contain any invalid characters or that it isn't stored corrupt in the database?
Stefan Gehrig
when i put the binary data into an file it works, we ve now found out, that the download is one byte larger than the file..
ArneRie
Perhaps you have an empty line after the closing `?>`-tag...
Stefan Gehrig
that was the problem, whitespace in an included class file.. what a day..
ArneRie