Jump to content

Cannot display word doc in browser


raydona

Recommended Posts

Hi,

  I’ve uploaded Microsoft Word docs to a MySql database and stored them as binary files. But when I download a file and try to display it in a browser (Firefox or IE) illegible characters are displayed. It seems that a file needs to be transformed back to a Word doc for it to be displayed correctly. Here is the PHP code for downloading a file:

<?php

  $con = mysql_connect("hostserver",“user","pswd");

  if(!$con)

  { die('Could not connect: ' . mysql_error());

  }

  mysql_select_db("database", $con);

 

  $sql = "SELECT name, type, size, content FROM CVs ".

                  "WHERE id = 3";

  $res = mysql_query($sql) or die('Error, query failed');

 

  list($name, $type, $size, $content) = mysql_fetch_array($res);

  header("Content-length: $size");

  header("Content-type: $type");

  header("Content-Disposition: attachment; filename=$name");

  echo $content;

  mysql_close($con);

  exit;

?>

I wonder if anyone has any suggestions as to how to rectify the problem. I am completely lost as to what to do next. I would be very grateful for all help.

 

Link to comment
https://forums.phpfreaks.com/topic/155235-cannot-display-word-doc-in-browser/
Share on other sites

why are you storing that in a database?

 

the overhead with storing files in a database is pretty big so rarely I'd do anything like that.

 

if its a case of storing the file securely maybe look at storing it behind the site root and forcing download rather than displaying in browser...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.