Cómo solicitar el cuadro de diálogo Guardar como después de crear un documento de Word con Fopen y Fwirte en PHP

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);

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);
header('Content-type: application/ms-word');
header('Content-disposition: attachment; filename="yourDoc.doc"');

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

//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//

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

Realmente necesito ayuda para esto.


preguntado el 28 de agosto de 11 a las 04:08

1 Respuestas

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.

Respondido 28 ago 11, 08:08

Thnx Marc B - I verified that there are no prior outputs, by executing the create file event to a separate php page. I tried using just the headers with yourDoc.doc already placed in the folder, but I cannot get a Save As prompt. $wordDoc does echo accordingly. I tried the entire script (i.e. fopen, fwrite, and headers), but the file still saved to the server and did not prompt me with a Save As dialog box. Do you have any other suggestions? Do I have to change the settings on my test server (LAMP)? - user175328

Using something like FF's Firebug/HTTPFox to view the headers the browser is receiving - check that the disposition header is actually coming through. Try a different browser to do attemp the download. Beyond that, no idea. - Marc B

Thnx again, I finally got it to work in FF. I guess you can only execute the file download with href, and not just an onclick JS function. Now I have to execute unlink(), to erase the erroneous file that automatically saves to the server. Unfortunately, Safari reads my script differently from FF, so Safari thinks the doc doesn't exist because of the unlink(), whereas FF gets the doc before the unlink() executes. Makes me wonder about IE... - user175328

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.