/    Sign up×
Community /Pin to ProfileBookmark

txt files show

hi

i had read about fopen in text files but i don´t know a lot of php, and i wanna know if i can put on my html website a txt file content.

can you tell me how??

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@GarySJun 23.2006 — Can you say more about what you're trying to achieve?
Copy linkTweet thisAlerts:
@P099authorJun 23.2006 — yes

i have a file in my server, for instance road.txt

i wanna show in my site the content of road.txt in a cell. If in the file said the history of roads in Germany, i wanna show that in the website, like regular text to the reader.

Please if you don´t understand tell me

i wanna show on the screen with a html format a txt file that doesn´t have a format only text.

Thanks
Copy linkTweet thisAlerts:
@GarySJun 23.2006 — I see... the text file is acting as a data source - like a very simple database. There are a number of was you could proceed:

Read the file - either line-by-line or the entire thing at once.

Include the file - you'd have to be a bit casreful about the content of the file, but it could be as easy as this:

[code=html]
<table>
<tr>
<td><?php include('textfile.txt') ?></td>
<td>
</table>
[/code]
Copy linkTweet thisAlerts:
@CharlesJun 23.2006 — Myself, I'd use [code=php]<td><?php echo file_exists ('textfile.txt') ? htmlspecialchars (file_get_contents ('textfile.txt')) : '&nbsp;' ?></td>[/code]
Copy linkTweet thisAlerts:
@NogDogJun 23.2006 — [EDIT]Charles more-or-less beat me to the punch while I was typing (and watching TV). ? [/EDIT]

In this case, just to make sure nothing in the text file messes up the document's HTML syntax (or even gets interpreted as being PHP code), I might do:
[code=php]
<td><?php echo htmlentities(file_get_contents("textifle.txt")); ?></td>
[/code]

Also, if you want to maintain the line breaks in the text file:
[code=php]
<td><?php echo nl2br(htmlentities(file_get_contents("textfile.txt"))); ?></td>
[/code]
Copy linkTweet thisAlerts:
@CharlesJun 23.2006 — As if we needed it, there's more proof the TV is bad for you.

I like the "n2br" but I don't like your use of double quotes. Double quotes support variable substitution while single quotes do not. That means that double quotes carry a much heavier overhead.
Copy linkTweet thisAlerts:
@the_treeJun 23.2006 — Really, is it actually a significant amount on small to medium sized sites? I tend to use single quotes when there's a choice anyway but never out of concern for server load.
Copy linkTweet thisAlerts:
@CharlesJun 23.2006 — Really, is it actually a significant amount on small to medium sized sites? I tend to use single quotes when there's a choice anyway but never out of concern for server load.[/QUOTE]Regarding server load, "Never increase, beyond what is necessary," that what I always say.
Copy linkTweet thisAlerts:
@the_treeJun 23.2006 — Nice comeback. But the sky won't start to fall if you use double quotes a few times.
Copy linkTweet thisAlerts:
@P099authorJun 23.2006 — ok

Thanks for the help but it doesn´t work.

I haven´t work with php before so i don´t know that much, but, i just copy what you wrote and put it on my code, in the html files, but still it doesn´t work.

Should i have to write something beside this. There is no problem with the file name or location, i already check that one

the code is not in div tags or p tags, it is only in the code you pass me.

Thanks again, and can you help me??

[CODE]</tr>
<tr>
<td width="10" rowspan="3">&nbsp;</td>
<td width="624" colspan="2">
<?php echo nl2br(htmlentities(file_get_contents("file.txt"))); ?>
</td>
<td width="30" rowspan="3">&nbsp;</td>
</tr>
<tr>
<td>[/CODE]
Copy linkTweet thisAlerts:
@CharlesJun 23.2006 — Give your file a ".php" extension and not ".html" and use the following just to be sure:[code=php]<?php echo file_exists ('file.txt') ? nl2br (htmlentities (file_get_contents ('file.txt'))) : '&nbsp;' ?>[/code]
Copy linkTweet thisAlerts:
@P099authorJun 23.2006 — ok, now it´s working great. Thanks
×

Success!

Help @P099 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...