Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9280833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:58:18+00:00 2026-06-18T17:58:18+00:00

I am trying to make a download action that downloads a Word doc generated

  • 0

I am trying to make a download action that downloads a Word doc generated in the ‘download’ controller using PHPDOCX. So far PHPDOCX is able to save the desired .docx file in the correct folder, but something goes wrong when I try to download it. Since Media Views were deprecated, I must use the CakeResponse file method as suggested in the CakePHP 2.x Cookbook:

// In the controller:
$this->response->file($file['path'], array('download' => true, 'name' => $filename));
return $this->response;

I was able to use this method for to export an RTF with no problem (the RTF was generated using PHPRTFLite), but when I use the method for a .docx file using PHPDOCX I receive the following error in Firefox:

The character encoding declaration of the HTML document was not found
when prescanning the first 1024 bytes of the file. When viewed in a
differently-configured browser, this page will reload automatically.
The encoding declaration needs to be moved to be within the first 1024
bytes of the file.

I would like to use a document generator that accepts HTML, which is why I chose PHPDOCX. Considering the above error, I set off to define the headers and content-type using the following method:

$this->response->header(array('Content-type'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document'));

But I still receive the same error in CakePHP:

The requested file APP/files/info_sheets/filename.docx was not found or not readable

One thing I was thinking is that PHPDOCX sends many errors when it generates the document and this is interfering with the Mime-type or encoding. But according to the 2.x Cookbook:

Headers are not sent when CakeResponse::header() is called either.
They are just buffered until the response is actually sent.

Another idea is that I need to set the character encoding in the header right after the content-type:

$this->response->header(array('Content-type'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'));

But this results in garbled text.

Does anyone have any ideas how to resolve this? The “download.ctp” view file is currently blank. Please let me know if you need additional information about this issue.

Thanks!

Chris

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T17:58:19+00:00Added an answer on June 18, 2026 at 5:58 pm

    First of all, you might try to disable autoRender, otherwise CakePHP might still try to render your view and layout;

    $this->autoRender = false;
    

    Also, haven’t tested it, but have you tried this to set the header:

    // register the type 
    $this->response->type(array('docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'));
    // set the type for the response
    $this->response->type('docx');
    

    See the documentation:
    http://book.cakephp.org/2.0/en/controllers/request-response.html#dealing-with-content-types

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to download a CSV file using HttpResponse to make sure that
I am trying to test my controller's create action. I am using Devise for
I am trying to make a script (batch in windows) that download 5 text
I'm trying to make sure that the user doesn't download a file that will
[edited] I am trying to make a script that downloads a file, the problem
I'm trying to make android download a zip file from my web server using
I was trying to make a sample code run download by the link http://www.magtek.com/support/software/downloads/sw/99510108.zip
I am trying to make a script that will let me download files outside
I am trying to make a cURL call to download a CSV file and
I'm trying to make it possible for the user to download an Excel spreadsheet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.