Help in retrieving the contents of a .txt file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
coder500
Forum Newbie
Posts: 20
Joined: Fri Jul 25, 2008 10:24 am
Location: Singapore

Help in retrieving the contents of a .txt file

Post by coder500 »

I want to place a text-file on the server so that it can be edited and updated. The contents of this text file need to be scrolled within a box on a web page. Is there anyway possible so that I can write the .txt document in html format, so that headings can be in bold or someother formatting I can apply. The code I have used is as follows.

Code: Select all

 
$file = fopen("welcome.txt", "r") or exit("Unable to open file!");
echo '<center>';
 echo '<table bgcolor="#efefef" width="500"><tr><td>';
 echo '<textarea name="stuff" cols="60" rows="10">';
 while(!feof($file))
  {
  echo fgets($file);
  }
fclose($file);
 
echo '</textarea><br>';
echo '</td></tr></table>';
 
The problem is if I write html code it will come as html. For example if I write <a href="">Check[/url] it will appear in the textarea as <a href="">Check[/url] and not as a link.
Any help, please...
BillBillJr
Forum Newbie
Posts: 9
Joined: Tue Aug 19, 2008 7:43 pm
Location: U.S.A.

Re: Help in retrieving the contents of a .txt file

Post by BillBillJr »

Here is your problem:

Code: Select all

 $file = fopen("welcome.[b]html[/b]", "r") or exit("Unable to open file!");
echo '<center>';
 echo '<table bgcolor="#efefef" width="500"><tr><td>';
 echo '<textarea name="stuff" cols="60" rows="10">';
 while(!feof($file))
  {
  echo fgets($file);
  }
fclose($file);
 
echo '</textarea><br>';
echo '</td></tr></table>';
Just change the format of the file (which I did above) and it should work fine.
Corvin
Forum Commoner
Posts: 49
Joined: Sun Dec 03, 2006 1:04 pm

Re: Help in retrieving the contents of a .txt file

Post by Corvin »

You can't format text within a textarea. You have to use a div-container. For Example:

Code: Select all

<div style="width: 300px; height: 150px; border: 1px solid #000000; background-color: #f4f4f4; overflow: auto; ">
 [<b>Your</b> <i>text</i> <u>here</u>.]
</div>
But of course you are not able to modify the text this way. What you are looking for is a WYSIWYG-Editor. Those are available as Java Applets for example.

Btw. it's much easier and less code using file_get_contents() and file_put_contents().
coder500
Forum Newbie
Posts: 20
Joined: Fri Jul 25, 2008 10:24 am
Location: Singapore

Re: Help in retrieving the contents of a .txt file

Post by coder500 »

Hi
what I am trying to do can be explained as follows.
I have a .txt file. I update it by editing in a textarea.(can u suggest a WYSIWG editor, I can use instead of this textarea)
In the code I mentioned I am trying to display the contents of .txt file.
The contents are coming. But all the html formatting I put through textarea is ignored...
Corvin
Forum Commoner
Posts: 49
Joined: Sun Dec 03, 2006 1:04 pm

Re: Help in retrieving the contents of a .txt file

Post by Corvin »

coder500 wrote:(can u suggest a WYSIWG editor, I can use instead of this textarea)
:arrow: Kafenio.editor
:arrow: Demo of Kafenio.editor
Post Reply