473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1506
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
1
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
2
by: Derik | last post by:
I've got a XML file I read using a file_get_contents and turn into a simpleXML node every time index.php loads. I suspect this is causing a noticeable lag in my page-execution time. (Or the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.