13 Replies - 15237 Views - Last Post: 25 February 2009 - 02:33 AM Rate Topic: -----

#1 ayman_mastermind   User is offline

  • human.setType("geek");
  • member icon

Reputation: 127
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Trying to display contents of a .txt file in an HTML page

Posted 23 February 2009 - 06:41 AM

Hey, everyone, i am very new to php which seems to be a very powerful and popular language on the web and seems to be crucial for me to now how to program in, i developing a website that needs constant update, for this reason i will be using php script to make the webpage get it's content from a text file that can be edited by the website admin without having to modify the html file and play with the code, he just has to change the contents of the text file and the contents of the page would change. Since i am new to php and i need to study it really fast becuase i am in great need for it, i started working on a example that would serve as a simple test to whether that can be achieved, the script gets the contents of the file and i supposed to print them into the page, the webpage opens yet the contents are not displayed although the web host i am using supports php, so please i would like you to check out the code and tell me what is wrong and give me some suggestions, thanks very much for your help :)

<?Php
$file = $_SERVER['DOCUMENT_ROOT'] . "content.txt"; 
$contents = file($file);
$string = implode($contents);
?>
<html>
<head></head>
<title>test</title>
<body>
<?php echo $string;?>
</body>
</html> 



Is This A Good Question/Topic? 0
  • +

Replies To: Trying to display contents of a .txt file in an HTML page

#2 noboost4you   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 171
  • Joined: 28-January 09

Re: Trying to display contents of a .txt file in an HTML page

Posted 23 February 2009 - 07:09 AM

View Postayman_mastermind, on 23 Feb, 2009 - 08:41 AM, said:

Hey, everyone, i am very new to php which seems to be a very powerful and popular language on the web and seems to be crucial for me to now how to program in, i developing a website that needs constant update, for this reason i will be using php script to make the webpage get it's content from a text file that can be edited by the website admin without having to modify the html file and play with the code, he just has to change the contents of the text file and the contents of the page would change. Since i am new to php and i need to study it really fast becuase i am in great need for it, i started working on a example that would serve as a simple test to whether that can be achieved, the script gets the contents of the file and i supposed to print them into the page, the webpage opens yet the contents are not displayed although the web host i am using supports php, so please i would like you to check out the code and tell me what is wrong and give me some suggestions, thanks very much for your help :)

<?Php
$file = $_SERVER['DOCUMENT_ROOT'] . "content.txt"; 
$contents = file($file);
$string = implode($contents);
?>
<html>
<head></head>
<title>test</title>
<body>
<?php echo $string;?>
</body>
</html> 



I'm no master coder by any means, but try changing the <?Php to lowercase <?php
Was This Post Helpful? 0
  • +
  • -

#3 thehat   User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Trying to display contents of a .txt file in an HTML page

Posted 23 February 2009 - 07:14 AM

When I run it I get a file not found error. I fixed that by changing

$file = $_SERVER['DOCUMENT_ROOT'] . "content.txt";



to

$file = "content.txt";


Was This Post Helpful? 0
  • +
  • -

#4 ayman_mastermind   User is offline

  • human.setType("geek");
  • member icon

Reputation: 127
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Trying to display contents of a .txt file in an HTML page

Posted 23 February 2009 - 07:28 AM

I tried both of your suggestions and that didn't solve the problem, anymore ideas and help is welcome, Thanks.
Was This Post Helpful? 0
  • +
  • -

#5 thehat   User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Trying to display contents of a .txt file in an HTML page

Posted 23 February 2009 - 07:37 AM

Funny, it worked for me. Is the text file in the same folder as the php?

Where are you testing this? If it's on your own server, you should turn on error reporting in php so you can get a better idea of what's going wrong. Speaking of which, apologies if this a stupid question for you but are you running this file on a local machine or from a server?
Was This Post Helpful? 0
  • +
  • -

#6 ayman_mastermind   User is offline

  • human.setType("geek");
  • member icon

Reputation: 127
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Trying to display contents of a .txt file in an HTML page

Posted 23 February 2009 - 07:46 AM

Quote

Funny, it worked for me. Is the text file in the same folder as the php?

Where are you testing this? If it's on your own server, you should turn on error reporting in php so you can get a better idea of what's going wrong. Speaking of which, apologies if this a stupid question for you but are you running this file on a local machine or from a server?

I have the html file on a subdomain that i am sure it's host supports php(i have configured that myself) and btw other php scripts work fine on the main site... and i am running the script from a server, not from local machine, although i would want to try to do that to test it, how can i run php script on the local machine on windows(i am new to php)? Thanks
Was This Post Helpful? 0
  • +
  • -

#7 thehat   User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Trying to display contents of a .txt file in an HTML page

Posted 23 February 2009 - 08:01 AM

Well, that's the thing. You can't unless your local machine has Internet Information Services running on it. If you have IIS then you need to install the php software and configure it. The advantage of taking the time to make this work is that you could then have full error reporting enabled. The disadvantage is that it can take quite a while to set up if you don't know what you're doing.

I have a good link for setting up php, which I shall now look for.
Was This Post Helpful? 0
  • +
  • -

#8 ayman_mastermind   User is offline

  • human.setType("geek");
  • member icon

Reputation: 127
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Trying to display contents of a .txt file in an HTML page

Posted 23 February 2009 - 08:13 AM

Quote

Well, that's the thing. You can't unless your local machine has Internet Information Services running on it. If you have IIS then you need to install the php software and configure it. The advantage of taking the time to make this work is that you could then have full error reporting enabled. The disadvantage is that it can take quite a while to set up if you don't know what you're doing.

I have a good link for setting up php, which I shall now look for.

Okay, man then if you can provide me with the link i will be more than happy, yet still if anyone spots an error or has a solution in the code above or has any suggestions please do tell me thanks... :)
Was This Post Helpful? 0
  • +
  • -

#9 thehat   User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Trying to display contents of a .txt file in an HTML page

Posted 23 February 2009 - 08:22 AM

This is my favourite tutorial for setting up php. It does assume you already have IIS6 (XP/2003) running, but that's pretty easy to do.

For your existing code, I tried it and it worked with the change I gave above. Are you sure you have the name of your text file correct?
Was This Post Helpful? 1
  • +
  • -

#10 ayman_mastermind   User is offline

  • human.setType("geek");
  • member icon

Reputation: 127
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Trying to display contents of a .txt file in an HTML page

Posted 23 February 2009 - 08:28 AM

Quote

This is my favourite tutorial for setting up php. It does assume you already have IIS6 (XP/2003) running, but that's pretty easy to do.

For your existing code, I tried it and it worked with the change I gave above. Are you sure you have the name of your text file correct?

Okay thanks for the link first of all, second i am sure that i have the content.txt file in the same directory and that it is the right file with the right name , i will set up php on my machine and i will see what i can do about the problem, thanks very much for the help everyone in general and in specific thehat :)
Was This Post Helpful? 0
  • +
  • -

#11 cam888   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 20-February 09

Re: Trying to display contents of a .txt file in an HTML page

Posted 23 February 2009 - 09:43 AM

Maybe try
<?php
$string = file_get_contents("content.txt"); 
?>
...
<?php 
echo $string;
?>
...


Was This Post Helpful? 0
  • +
  • -

#12 Kirth   User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 157
  • Joined: 09-July 08

Re: Trying to display contents of a .txt file in an HTML page

Posted 24 February 2009 - 07:59 AM

Why do it the hard way when you can do it the easy way?

<?php
include('text.txt');
?>


Was This Post Helpful? 0
  • +
  • -

#13 cam888   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 20-February 09

Re: Trying to display contents of a .txt file in an HTML page

Posted 24 February 2009 - 08:51 AM

That way will work if he ever wants to change the data slightly, e.g. he made a bbcode parser. Either way they should both work.
Was This Post Helpful? 0
  • +
  • -

#14 Valek   User is offline

  • The Real Skynet
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Trying to display contents of a .txt file in an HTML page

Posted 25 February 2009 - 02:33 AM

If you don't have IIS, you can always go with XAMPP as well. It's what I use, myself.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1