LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-31-2005, 02:37 PM   #1
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Rep: Reputation: 45
php display a file, driving me insane


Hi,

This is driving me insane. All i want to do is just display the contents of a file on a .php page.. But not messing up any indentation or anything.. (just like directly reading from the file)

I tried absolutely everything, but the closest i could ever get to is displaying everything in one line..

So far i have,
Code:
<?php
    $listFile = "Changelog";
    if (!($fp = fopen($listFile, "r")))  
        exit("Unable to open the input file, $listFile.");
    $buffer = fread($fp, filesize($listFile));  
    print "$buffer<br>\n";  
    fclose($fp);
?>
Which i managed to find online, but it still doesn't display the file properly.. I don't know what's wrong, please help.

Thanks
 
Old 01-31-2005, 03:11 PM   #2
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Rep: Reputation: 32
I know enough PHP & HTML to be slightly dangerous but I think your problem the fact that Browsers/HTML ignore whitespace.
So in HTML you can write:
Code:
<p>                        There is a lot of whitespace there!</p>
What you will see is:
Code:
 There is a lot of whitespace there!
With only one space. HTML ignore consectuive whitespace.
Oh...
Wait...
try pre & post -pending a <pre> tag.
Like so...
Code:
<pre>
<?php
    $listFile = "Changelog";
    if (!($fp = fopen($listFile, "r")))  
        exit("Unable to open the input file, $listFile.");
    $buffer = fread($fp, filesize($listFile));  
    print "$buffer<br>\n";  
    fclose($fp);
?> 
</pre>
That is sort of like the "[code]" tag used on this forum.

See what that does...
 
Old 01-31-2005, 03:17 PM   #3
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Original Poster
Rep: Reputation: 45
Genius

I wouldn't have guessed this without your help.. well, not for the next 3 nights. One annoying thing though.. The file contains some email addresses in the form of
<email@email.com>
, and they're not showing up at all ?! I'm guessing it's not liking the use of < >
 
Old 01-31-2005, 03:19 PM   #4
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Rep: Reputation: 32
Can you post some typical contents of the file?
 
Old 01-31-2005, 03:46 PM   #5
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Original Poster
Rep: Reputation: 45
Code:
......
		skipped AAlib 1.4rc5 (needs Xorg, GPM)
		skipped Imlib2 (needs Xorg)

2005-01-29   <root@xushitop.xushi.co.uk>

	* xushi: Started on Security
		OpenSSL 0.9.7e
		cracklib 2.7
		Linux-PAM 0.78
		Shadow 4.0.4.1 (Reinstalled patching 
			to fix linking against PAM)
		iptables 1.2.11
		skipped iptables for now
		GnuPG 1.4.0
		skiped Tripwire-portable 0.9 (libexec error)
.........
It would display everything, except for <root@xushitop.xushi.co.uk>
 
Old 01-31-2005, 04:03 PM   #6
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Rep: Reputation: 32
OK...
If you do a "View source of your PHP page you'll see the address is there but the angle brakcets make it look like a tag to the browser.
Strip out the brackets and your're in business...
Code:
<pre>
<?php
    $listFile = "data.txt";
    if (!($fp = fopen($listFile, "r")))
        exit("Unable to open the input file, $listFile.");
    $buffer = fread($fp, filesize($listFile));            
    $gtlt = array("<", ">");
    $buffer = str_replace($gtlt, "", $buffer);                       
    print "$buffer<br>\n";
    fclose($fp);
?>
</pre>
Cheers!
 
Old 01-31-2005, 04:06 PM   #7
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
If you have no HTML at all being displayed for the page and it is all a plain text file, you also might try putting this at the top:

<?header("Content-Type:text/plain");?>

Otherwise, you might have to replace all instances of < with &lt; and > with &gt; using something like str_replace.
 
Old 01-31-2005, 04:08 PM   #8
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Original Poster
Rep: Reputation: 45
I don't mind that at all.

Again, thank you for your help cmfarley19, deiussum

Last edited by xushi; 01-31-2005 at 05:24 PM.
 
Old 01-31-2005, 05:09 PM   #9
pnellesen
Member
 
Registered: Oct 2004
Location: Missouri, USA
Distribution: Slackware 12.2, Xubuntu 9.10
Posts: 371

Rep: Reputation: 31
Quick question: Is there a reason you wouldn't simply use an "include('$filename')" command? Just curious - I use this alot to spit out chunks of HTML on a web page; I assume you're wanting to do the same thing?

Apologies if I've not understood what you're trying to do
 
Old 01-31-2005, 05:24 PM   #10
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Original Poster
Rep: Reputation: 45
pnellesen, deiussum, I followed the above code because i've never programmed php before.

All i need to do is this. I have a changelog in a text file, which i want to display to the users. So instead of just displaying dull text from a textfile, say
mysite.com/file.txt
i want to stick it in an html file, surround it with a frame, make it look sexier..

So far, the php code above works. However, if you all think there are better ways in doing so, i'm open for suggestions.
 
Old 01-31-2005, 05:40 PM   #11
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
There is htmlentities() to encode special chars, like
PHP Code:
<pre>
<?php echo htmlentities(file_get_contents("Changelog"1)); ?>
</pre>
 
Old 02-01-2005, 07:04 AM   #12
cmfarley19
Member
 
Registered: Nov 2002
Location: Central VA
Distribution: Ubuntu/Debian
Posts: 228

Rep: Reputation: 32
Xushi,
I forgot about the htmlentities() function that keefaz suggests.
I think this is a cleaner version of what you already have:
Code:
<?php
    $listFile = "data.txt";
    if (!($fp = fopen($listFile, "r")))
        exit("Unable to open the input file, $listFile.");
    $buffer = fread($fp, filesize($listFile));
    $buffer = htmlentities($buffer);
    print "$buffer<br>\n";
    fclose($fp);
?>
 
Old 02-01-2005, 06:22 PM   #13
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Original Poster
Rep: Reputation: 45
Thank you,

I've added <pre> </pre> to the code above, and it works nicely.

While i still have the interest of some talented programmers Is there a way i can increase the font size of the output, without interfearing with the original text file?

And mind you, this probably is the last question i'll ask for now

I meen, take a base php file of
Code:
<body>
  <pre>
  <?php
    $listFile = "Changelog";
    if (!($fp = fopen($listFile, "r")))
        exit("Unable to open the input file, $listFile.");
    $buffer = fread($fp, filesize($listFile));
    $buffer = htmlentities($buffer);
    print "$buffer<br>\n";
    fclose($fp);
?>
</pre>
</body>
If i change it to,
Code:
<body> <font size="3" face="Verdana" class="text"><span class="style1"> <pre>
  <?php
    $listFile = "Changelog";
    if (!($fp = fopen($listFile, "r")))
        exit("Unable to open the input file, $listFile.");
    $buffer = fread($fp, filesize($listFile));
    $buffer = htmlentities($buffer);
    print "$buffer<br>\n";
    fclose($fp);
?>
</pre> </font> </body>
It would still display the resulting output the same original size it is in.
 
Old 02-02-2005, 03:44 AM   #14
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
add a css style :
Code:
pre { font-size: 18px; }
or in the <pre> markup like :
Code:
<pre style="font-size: 18px">
...
</pre>
Also why not use file_get_contents() instead of fopen, fread, fclose... seem quicker to me

Last edited by keefaz; 02-02-2005 at 03:45 AM.
 
Old 02-02-2005, 04:08 AM   #15
xushi
Senior Member
 
Registered: Jun 2003
Location: UK
Distribution: Gentoo
Posts: 1,288

Original Poster
Rep: Reputation: 45
Thanks, that worked.

Well, i know what to read about now. I should look into .css scripting, and a bit of php too.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
RPMs are driving me insane gauge73 Linux - Newbie 11 05-05-2005 01:02 PM
Samba driving me insane Lorend Linux - Newbie 7 02-16-2005 06:26 PM
Mplayer Driving Me Insane!!!!! njschroe Linux - Software 7 01-24-2005 05:31 PM
cvswinex driving me insane araldit Linux - Newbie 0 07-02-2004 05:05 PM
Sendmail is driving me insane. Please help me. ecroskey Linux - Newbie 1 03-02-2003 06:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration