display contents of .txt file using php
Asked Answered
B

9

5

using this code

<?php
foreach (glob("*.txt") as $filename) {   
    $file = $filename;
    $contents = file($file); 
    $string = implode($contents); 
    echo $string;
    echo "<br></br>";
}
?>

i can display the contants of any txt file in the folder the problem is all the formating and so on from the txt file is skipped

the txt file looks like

#nipponsei @ irc.rizon.net presents:

Title: Ah My Goddess Sorezore no Tsubasa Original Soundrack
Street Release Date: July 28, 2006

------------------------------------

Tracklist:

1. Shiawase no Iro On Air Ver
2. Peorth
3. Anata ni Sachiare
4. Trouble Chase
5. Morisato Ka no Nichijou
6. Flying Broom
7. Megami no Pride
8. Panic Station
9. Akuryou Harai
10. Hore Kusuri
11. Majin Urd
12. Hild
13. Eiichi Soudatsusen
14. Goddess Speed
15. Kaze no Deau Basho
16. Ichinan Satte, Mata...
17. Eyecatch B
18. Odayaka na Gogo
19. Heibon na Shiawase
20. Kedarui Habanera
21. Troubadour
22. Awate nai de
23. Ninja Master
24. Shinobi no Okite
25. Skuld no Hatsukoi
26. Kanashimi no Yokan
27. Kousaku Suru Ishi
28. Dai Makai Chou Kourin
29. Subete no Omoi wo Mune ni
30. Invisible Shield
31. Sparkling Battle
32. Sorezore no Tsubasa
33. Yume no Ato ni
34. Bokura no Kiseki On Air Ver

------------------------------------

Someone busted in, kicked me and asked why there was no release
of it. I forgot! I'm forgetting a lot...sorry ;_;

minglong

i the result i get looks like

#nipponsei @ irc.rizon.net presents: Title: Ah My Goddess Sorezore no Tsubasa Original Soundrack Street Release Date: July 28, 2006 ------------------------------------ Tracklist: 1. Shiawase no Iro On Air Ver 2. Peorth 3. Anata ni Sachiare 4. Trouble Chase 5. Morisato Ka no Nichijou 6. Flying Broom 7. Megami no Pride 8. Panic Station 9. Akuryou Harai 10. Hore Kusuri 11. Majin Urd 12. Hild 13. Eiichi Soudatsusen 14. Goddess Speed 15. Kaze no Deau Basho 16. Ichinan Satte, Mata... 17. Eyecatch B 18. Odayaka na Gogo 19. Heibon na Shiawase 20. Kedarui Habanera 21. Troubadour 22. Awate nai de 23. Ninja Master 24. Shinobi no Okite 25. Skuld no Hatsukoi 26. Kanashimi no Yokan 27. Kousaku Suru Ishi 28. Dai Makai Chou Kourin 29. Subete no Omoi wo Mune ni 30. Invisible Shield 31. Sparkling Battle 32. Sorezore no Tsubasa 33. Yume no Ato ni 34. Bokura no Kiseki On Air Ver ------------------------------------ Someone busted in, kicked me and asked why there was no release of it. I forgot! I'm forgetting a lot...sorry ;_; minglong
Bidden answered 29/5, 2009 at 21:10 Comment(1)
u used file_get_contents too, and it still looses formating file_get_contentsBidden
S
9

The implode defaults to an empty string. You should call implode something like this:

  $string = implode("<br>", $contents);
Squadron answered 29/5, 2009 at 21:13 Comment(0)
C
8

You have to add HTML line break elements to the physical line breaks. You could use the nl2br function to do that:

foreach (glob("*.txt") as $filename) {
    echo nl2br(file_get_contents($filename));
    echo "<br></br>";
}

Additionally I would use the file_get_contents function rather than the combination of file and implode.

Cyte answered 29/5, 2009 at 21:13 Comment(3)
I don’t like going a long way round. But using implode in this case would be fine as well.Cyte
@Bidden simplicity, nl2br basicly does the same as impload('<br />', $var) but it requires less arguments and therefor looks cleaner in the code so why not just use it.Endora
Implode in this case probably performs better. nl2br has no idea how many new lines there are and so probably has to use a string builder to make the output string. Implode can just allocate filesize + length ('<br>')*(count(lines)-1) chars immediately and then copy the data into place.Chafin
P
4

If this isn't part of an HTML document, you need to change the content type:

<?php
header("Content-Type: text/plain");
foreach (glob("*.txt") as $filename) { 
  readfile($filename);
}
?>

If it is part of an HTML document, just do this:

<pre>
<?php
foreach (glob("*.txt") as $filename) { 
  readfile($filename);
}
?>
</pre>

Alternatively you can replace newlines with breaks:

<?php
foreach (glob("*.txt") as $filename) { 
  $str = file_get_contents($filename);
  echo preg_replace('!\r?\n!', '<br>', $str);
}
?>
Practically answered 29/5, 2009 at 21:20 Comment(2)
You don't want the content type in the second and third examples.Roof
By far the most underrated answer for this question.Deberadeberry
O
1

embed the text file content between <pre></pre> tags

Orientation answered 29/5, 2009 at 21:13 Comment(1)
That probably won't be enough.Squadron
F
1

As several of the other responses mentioned, it greatly depends upon the page in which you're displaying the output.

Raw Text Output

If you're not adding any other content or HTML to the page. Simply change the HTTP Content-Type header to "text/plain"; that is:

header('Content-Type: text/plain');
echo file_get_contents('path/to/file');

As always, HTTP headers must be sent before any content is sent to the browser.

(X)HTML Output

Replacing \n's with <br/> will not fix whitespace truncation issues; that is, the removal of adjacent spaces and/or tabs. The easiest way to get around this, also as previously mentioned, is to use the <pre> tag to enclose the contents of the file. Unfortunately, this is not enough to satisfy XHTML. There are a number of symbols that are invalid in XML unless they are properly escaped, notably including: &, <, and >.

Thankfully, this is also an easy fix using the str_replace method:

$raw = file_get_contents('path/to/file');
echo '<pre>';
echo str_replace($raw, array('>','<','&','%'), array('&gt;','&lt;','&amp;','&#37;'));
echo '</pre>';
Fuliginous answered 31/5, 2009 at 0:38 Comment(0)
M
0

file() returns an array with the lines of the file.

If you implode those without glue there will be no linebreaks at all.

So, either get the contents unmodified using file_get_contents() (which gives you a string), or glue the implode with newline or

Meseems answered 29/5, 2009 at 21:16 Comment(0)
C
0

Peter Stuifzand had the right idea, passing a second argument to the implode function, so I won't address that. What I will point out is that your own echo "<br></br>"; code does not produce valid HTML. If you're doing HTML and want 2 line breaks, do echo "<br><br>"; and if you're doing XHTML and want 2 line breaks, do echo "<br/><br/>";. Otherwise, if you only want 1 line break, the HTML br tag does not have a closing tag, so </br> is not necessary in either case.

Condom answered 29/5, 2009 at 21:20 Comment(0)
G
0

write your text in a .txt file and redirect to url corresponding to that file

php example code

contents of allow.txt are

Authorized=True
Duration=1
OutputAnalog=NO_PLAYBACK
OutputDigital=NO_PLAYBACK

contents of deny.txt are

Authorized=False
Duration=0
OutputAnalog=NO_PLAYBACK
OutputDigital=NO_PLAYBACK

contents of php file

<?php
$user = $_REQUEST['username'];
$pass = $_REQUEST['password'];
$contentId = $_REQUEST['contentId'];
ob_start(); // ensures anything dumped out will be caught

 // do stuff here
allowUrl = 'http://localhost/allow.txt'; // this can be set based on whatever
$denyUrl = 'http://localhost/deny.txt';
// clear out the output buffer
while (ob_get_status())
{
    ob_end_clean();
}

// no redirect
if($user == "xyz" && $pass == "xyz")
header( "Location: $allowUrl" );
else
header("Location: $denyUrl");
?> 
Gunman answered 13/10, 2014 at 6:18 Comment(0)
F
0

Or you could just put it into a textarea like this:

<?
$file = 'file.txt';
$contents = file($file); 
$string = implode("",$contents); 
echo '<textarea readonly style="width:100%; height:200px;">';
echo $string;
echo "</textarea><br></br>";
?>

But only if you can and it turns out right.

Fein answered 21/4, 2015 at 16:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.