Question

Background

I have an application which uses php to print information from a mySQL line, into ms-word... It was working on another part of my website, but after new implementation... my browsers seem to want to download the page as an actual document, rather than give the option to open/save in MS Word... Was wondering if anyone saw an actual problem with the code here... No errors are being displayed in my logs, and full error_reporting is on

Code Excerpt

<?php
$id= "1";
$type= "failValidate";

if ($type=="failValidate"){

    $query_letters = "SELECT * FROM table WHERE id=$id";
    try {
        $letters = $customer->runQuery($query_letters);
        header("Content-type: application/vnd.ms-word");
        foreach($letters as $letter) {
            $filename = "letter.doc";
            header("Content-Disposition: attachment;Filename=" . $filename);
            echo $letters[0]['letter'];
        }   
    } catch (Exception $e) {
        echo "Error: " . $e->getMessage();
    }
}
?>

As of right now, all that happens when I link to run this php script, it gives me an open/save for the actual PHP file...which of course does not have the intended functionality.

Was it helpful?

Solution

Try to print it without a loop.

    $letters = $customer->runQuery($query_letters);
    $filename = "letter.doc";
    header("Content-type: application/vnd.ms-word");
    header("Content-Disposition: attachment; filename=" . $filename);
    echo $letters[0]['letter'];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top