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

  • SEARCH
  • Home
  • 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 6374093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:26:43+00:00 2026-05-25T01:26:43+00:00

I wrote the following script so that an end-user can create a word document

  • 0

I wrote the following script so that an end-user can create a word document from text entered into a textarea:

$fp = fopen("yourDoc.doc", 'w+');
fwrite($fp, $wordDoc);
fclose($fp);

Basically, the file being created is “yourDoc.doc”, and the text written to the file is located in the $wordDoc variable.

Currently, this script creates the document and automatically saves it to the same path where my server pages are located. What I want to do is use the above script, but prompt a “Save As” dialog box so that the end-user can save the document to their computer locally.

After a few hours of research, I saw that I could use headers, however I am unable to make them work. I cannot seem to find an example that includes headers, fopen, and fwrite altogether. For example, I tried many variations of the following script with no luck:

$fp = fopen("yourDoc.doc", 'w+');
fwrite($fp, $wordDoc);
fclose($fp);
header('Content-type: application/ms-word');
header('Content-disposition: attachment; filename="yourDoc.doc"');
readfile("yourDoc.doc");

I also tried the following (this is my entire php page):

<?php
//Connect to Db//
$con = mysql_connect("localhost", "root", "admin");
//Verify connection//
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}
//Select Db//
mysql_select_db("schooldb", $con);
$wordDoc = $_GET['word'];//text for word doc//
header('Content-type: application/ms-word');
header('Content-disposition: attachement; filename="yourDoc.doc"');
$fp = fopen("yourDocument.doc", 'w+');//word doc created//
fwrite($fp, $wordDoc);//text written to word doc//
fpassthru($fp);
fclose($fp);
mysql_close($con);  
?>

Does anyone see something wrong with this? The file ends up on my server, and I do not get a Save As dialog box.

I really need help on this.

Thanks,

  • 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-05-25T01:26:43+00:00Added an answer on May 25, 2026 at 1:26 am

    There’s no need to write the word data out to a file first:

    header('Content-type: application/ms-word');
    header('Content-disposition: attachment; filename="yourDoc.doc"');
    echo $wordDoc;
    

    however, you have to ensure that no output whatsoever has occured before you do the header() calls. PHP will NOT send headers if even a single character of “output” has occured, and this will fail.

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

Sidebar

Related Questions

I'm trying write a Ruby script that checks if user credentials are valid using
Here is the script that i wrote and it has weird syntax error at
Can anyone spot why the following script is not printing the passed arguments? import
I am trying to extract the string from a file having following pattern within
First the background: In Firefox 3.6.3 on Mac OS X 10.5.8 when entering text
Using php I'd like to subtract/minus two times; each time within a separate document
I am trying to create a capistrano task to setup my log rotation file.
Let me start by the end and the actual question: I'm trying to write
Thinking that to solve a problem I've got this is the fastest solution: Generate
This is my first attempt at AJAX, and I'm running into three specific problems.

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.