469,626 Members | 1,369 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

Reading from file

I have following code for putting contenst of a file into a web page:
<?php
$filename = "../msd/news.txt";
$handle = fopen ($filename, "r");
$content = fread ($handle, filesize($filename));
fclose($handle);
echo ($content);
?>
But in file news.txt I have formating that is not showing, but
interpreting.
For example, in that file there is:
<a href="link.php">link</a>
End result is hyperlink.

Can you tell me what I did wrong?
--
..:Dalibor:.
Feb 3 '06 #1
8 1400
>I have following code for putting contenst of a file into a web page:
<?php
$filename = "../msd/news.txt";
$handle = fopen ($filename, "r");
$content = fread ($handle, filesize($filename));
fclose($handle);
echo ($content);
?>
But in file news.txt I have formating that is not showing, but
interpreting.
If you output the content of a file in the middle of a bunch
of HTML, it will be interpreted as HTML.
For example, in that file there is:
<a href="link.php">link</a>
End result is hyperlink.

Can you tell me what I did wrong?


I suggest something like:

echo (htmlentities($content));

Although I don't think that will deal with newlines being interpreted
as going to a new line, vs. HTML ignoring line breaks.

Gordon L. Burditt
Feb 3 '06 #2
NC
Dalibor wrote:

I have following code for putting contenst of a file into a web page:
<?php
$filename = "../msd/news.txt";
$handle = fopen ($filename, "r");
$content = fread ($handle, filesize($filename));
fclose($handle);
echo ($content);
?>
But in file news.txt I have formating that is not showing, but
interpreting.
For example, in that file there is:
<a href="link.php">link</a>
End result is hyperlink.

Can you tell me what I did wrong?


You forgot to output <pre> tags. Also, you should know that there is a
much faster and easier way of outputting a text file; you should use
readfile(). So your code should look like this:

$filename = "../msd/news.txt";
echo "<pre>\r\n";
readfile($filename);
echo "</pre>\r\n";

Cheers,
NC

Feb 4 '06 #3
On Fri, 3 Feb 2006 23:52:34 +0100, Dalibor wrote:
I have following code for putting contenst of a file into a web page:
<?php
$filename = "../msd/news.txt";
$handle = fopen ($filename, "r");
$content = fread ($handle, filesize($filename));
fclose($handle);
echo ($content);
?>
But in file news.txt I have formating that is not showing, but
interpreting.
For example, in that file there is:
<a href="link.php">link</a>
End result is hyperlink.

Can you tell me what I did wrong?


This worked:
<textarea>
<?php
$filename = "../msd/news.txt";
$handle = fopen ($filename, "r");
$content = fread ($handle, filesize($filename));
fclose($handle);
echo ($content);
?>
</textarea>

Thanks everyone for helping!
--
..:Dalibor:.
Feb 6 '06 #4
On Fri, 3 Feb 2006 23:52:34 +0100, Dalibor wrote:
I have following code for putting contenst of a file into a web page:
<?php
$filename = "../msd/news.txt";
$handle = fopen ($filename, "r");
$content = fread ($handle, filesize($filename));
fclose($handle);
echo ($content);
?>
But in file news.txt I have formating that is not showing, but
interpreting.
For example, in that file there is:
<a href="link.php">link</a>
End result is hyperlink.

Can you tell me what I did wrong?


I find solution:
<textarea>
<?php
$filename = "../msd/news.txt";
$handle = fopen ($filename, "r");
$content = fread ($handle, filesize($filename));
fclose($handle);
echo ($content);
?>
</textarea>
--
..:Dalibor:.
Feb 6 '06 #5

"Gordon Burditt" <go***********@burditt.org> wrote in message
news:11*************@corp.supernews.com...
I have following code for putting contenst of a file into a web page:
<?php
$filename = "../msd/news.txt";
$handle = fopen ($filename, "r");
$content = fread ($handle, filesize($filename));
fclose($handle);
echo ($content);
?>
But in file news.txt I have formating that is not showing, but
interpreting.
If you output the content of a file in the middle of a bunch
of HTML, it will be interpreted as HTML.
For example, in that file there is:
<a href="link.php">link</a>
End result is hyperlink.

Can you tell me what I did wrong?


I suggest something like:

echo (htmlentities($content));


you could try
echo nl2br(htmlentities($content));

which gives me
<?php echo nl2br(htmlentities("\"\n\"")); ?>
&quot;<br />
&quot;

(note that the newlines are still in there)
the 5.0 manual states that it uses xhtml's <br />. someone else stated it
didn't (maybe they were using an earlier version?).

Although I don't think that will deal with newlines being interpreted
as going to a new line, vs. HTML ignoring line breaks.

Gordon L. Burditt

Feb 12 '06 #6

"NC" <nc@iname.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
Dalibor wrote:

I have following code for putting contenst of a file into a web page:
<?php
$filename = "../msd/news.txt";
$handle = fopen ($filename, "r");
$content = fread ($handle, filesize($filename));
fclose($handle);
echo ($content);
?>
But in file news.txt I have formating that is not showing, but
interpreting.
For example, in that file there is:
<a href="link.php">link</a>
End result is hyperlink.

Can you tell me what I did wrong?
You forgot to output <pre> tags. Also, you should know that there is a
much faster and easier way of outputting a text file; you should use
readfile(). So your code should look like this:

$filename = "../msd/news.txt";
echo "<pre>\r\n";


even if you use pre, you still must use htmlentities() to produce valid HTML
(like on > and < and "). checked it out on 2 validators. doesn't make a
diff whether it's code or pre.
readfile($filename);
echo "</pre>\r\n";

Cheers,
NC

Feb 12 '06 #7

"Dalibor" <da********@lycos.com> wrote in message
news:61****************************@40tude.net...
On Fri, 3 Feb 2006 23:52:34 +0100, Dalibor wrote:
I have following code for putting contenst of a file into a web page:
<?php
$filename = "../msd/news.txt";
$handle = fopen ($filename, "r");
$content = fread ($handle, filesize($filename));
fclose($handle);
echo ($content);
?>
But in file news.txt I have formating that is not showing, but
interpreting.
For example, in that file there is:
<a href="link.php">link</a>
End result is hyperlink.

Can you tell me what I did wrong?
This worked:
<textarea>
<?php
$filename = "../msd/news.txt";
$handle = fopen ($filename, "r");
$content = fread ($handle, filesize($filename));
fclose($handle);
echo ($content);
?>


that could have all been done with readfile(). another cool function is
file_get_contents() - all of them I think work with URLs also.
</textarea>

Thanks everyone for helping!
--
.:Dalibor:.

Feb 12 '06 #8

"Jim Michaels" <jm******@nospam.yahoo.com> wrote in message
news:ps********************@comcast.com...

"Dalibor" <da********@lycos.com> wrote in message
news:61****************************@40tude.net...
On Fri, 3 Feb 2006 23:52:34 +0100, Dalibor wrote:
I have following code for putting contenst of a file into a web page:
<?php
$filename = "../msd/news.txt";
$handle = fopen ($filename, "r");
$content = fread ($handle, filesize($filename));
fclose($handle);
echo ($content);
?>
But in file news.txt I have formating that is not showing, but
interpreting.
For example, in that file there is:
<a href="link.php">link</a>
End result is hyperlink.

Can you tell me what I did wrong?
This worked:
<textarea>
<?php
$filename = "../msd/news.txt";
$handle = fopen ($filename, "r");
$content = fread ($handle, filesize($filename));
fclose($handle);
echo ($content);
?>


that could have all been done with readfile(). another cool function is
file_get_contents() - all of them I think work with URLs also.

I forgot one thing. in a textarea, using
echo htmlentities(file_get_contents("filepath"))
is probably a good idea, if you intend to display the content on a web page
or sending HTML email, which is often the case. if you are sending plain
text email, readfile() should be just fine.
</textarea>

Thanks everyone for helping!
--
.:Dalibor:.


Feb 12 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Xah Lee | last post: by
1 post views Thread by fabrice | last post: by
19 posts views Thread by Lionel B | last post: by
4 posts views Thread by Oliver Knoll | last post: by
6 posts views Thread by Rajorshi Biswas | last post: by
7 posts views Thread by John Dann | last post: by
6 posts views Thread by efrenba | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.