Jump to content

Displaying Text Documents


ironside82

Recommended Posts

Hi,

 

I've a question on how to display a text document using PHP,

 

I want to be able to display a formatted text document into part of my site using PHP, It doesn't matter what format the document is rtf, doc etc..

 

The file name of the text document is stored in a database and then recalled using PHP and displayed with all its correct formatting.

 

I've tried using file_get_contents to recall an RFT file but that churns out a load of rubbish.

 

As long as I can recall a file from a database and display it correctly I don't mind how it is done.

 

Thanks in advance for you help

 

James

Link to comment
Share on other sites

This isn't easy to do to support any format (or many different ones). PDF, RTF, DOC, etc. are all formatted, they aren't in plain text. PHP has a vairety of classes, libraries and extensions available on the net that you may be able to use, but each is likely to only support 1 format.

Link to comment
Share on other sites

I've been writing a DOCX and XML file to text converter in C. While I was doing some reading about XML conversion and parsing there was also a paragraph on RTF files.

 

From what I can remember, parsing RTF files is extremely difficult to do.

 

Example code of an RTF file from Wikipedia:

 

{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard
This is some {\b bold} text.\par
}

Link to comment
Share on other sites

the first question should be WHO is creating the document

 

I don't think it should by WHO, but for what purpose. What will the file contain?

 

I dont need to support multiple formats

 

My bad. I misunderstood the following to mean it doesn't matter what format the user provides.

 

It doesn't matter what format the document is rtf, doc etc..
Link to comment
Share on other sites

If you don't need fancy formating then stick with text.  If you do then HTML may be fine as it's easy for people to create in Word, etc.. and is easy to display:

 

$conn = mysql_connect('host', 'user', 'password');
$result = mysql_query("SELECT `filename` FROM `dbname`.`tablename` WHERE something=something LIMIT 1");
$row = mysql_fetch_assoc($result);
echo "<pre>" . file_get_contents($row['filename']) . "</pre>";  // for text files
echo file_get_contents($row['filename']); // for HTML files

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.