Jump to content

How can I read a ".txt" file in an HTML format using PHP?


APD1993

Recommended Posts

I am creating a series of PHP scripts that allow for someone to append extra information to an Accident Claim Report. One of these scripts lists the files in a directory called the RCM directory. For the purpose of this, there is only one file in the list named "denman2.txt" and a hyperlink is attached to it so that the updated Accident Claim Report can be displayed. However, I would like it so that a style sheet could be applied to the "denman2.txt" text file, and I was wondering how it would be possible for the "denman2.txt" file to be opened in a format such as an HTML format so that a style sheet can be applied to it.

 

Here is my Append Code:

 

<html>
<head><title>Amend File</title></head>
<?php

if($_POST['append'] !=null)
{
$filename="C:/xampp/htdocs/rcm/denman2.txt";
$file=fopen($filename, "a");
$msg="Updated Information: " .$_POST['append']. "<br>";
fputs ($file, $msg);
fclose($file);
}
?>
<body>
<h1>Do you want to append to a document?</h1>
Enter Updated Information:
<form action="amendfile2.php" method="post">
<input type="text" size="40" name="append"><br><br>
<input type="submit" value="Add updated information to report">
</form>
<form action="viewfile3.php" method="post">
<input type="submit" value="View Web Blog">
</form>
</body></html>

 

My View File Code:

 

<html><head>
<title>RCM File List</title></head>
<body>
<h1>Files in RCM Directory</h1>
<?php
$dir="C:/xampp/htdocs/rcm/*txt";


foreach(glob($dir) as $file)
{
$list = "<li><a href='readfile2.php'>Filename: $file</a></li>";
}
?>

<ul>
<?php echo $list; ?>
</ul>
</body>
</html>

 

And my Read File Code:

 

<html><head><title>Reading Accident Reports</title></head>
<body>
<?php

function myErrorHandler($errno, $errstr) 
{
echo "<b>Error: </b> [$errno] $errstr";
echo "<br>";
echo "End scripting";
die();
}
set_error_handler("myErrorHandler");
$filename="C:/xampp/htdocs/rcm/denman2.txt";
$file = fopen($filename, "r");
while (!feof($file)) {
$line=fgets($file,1024);
echo "$line<br>";
}
fclose($file);
?>
</body>
</html>

 

Any help is appreciated :D

Link to comment
Share on other sites

Does the txt file already contain any markup? Do you have an example of a typical txt file? Who is creating the txt file. How are they creating the txt file?

 

The basic text file is hard typed by me and it consists of  :

 

First Name: First Name goes here

Last Name: Last Name goes here

Age: 18

Complete Weeks Since Accident: 2<br>

 

Can I use <h1> and <p> tags in the text file for a similar effect to HTML (i.e. the <h1> tag will make a level 1 heading in a text file?)?

Link to comment
Share on other sites

in layman's terms (that me!) primarily how the server and browser decide how to handle it based upon its extension.

example: if you name a file index.html, a second file named index.php and a third file named index.txt (all containing the exact same content). the server gets the html file. if you delete the html file, then the server gets the php file. finally, if you delete the php file, the server will NOT get the txt file

 

Clear as mud? ;)

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.