Log in

View Full Version : Script to display contents of a text file



CraigL
04-08-2007, 12:01 AM
I have a CGI script that increments a "counter" and writes the contents of counter to "counter.txt". This counter.txt file consists of one line and the data is a number. I want to display this number on a web page using a script. Seems simple, but I haven't come accross a script in my search.

Thanks in advance for any help.
CL

djr33
04-08-2007, 12:11 AM
Using PHP, this is quite easy.

Include the following line anywhere in your page, and it will be displayed there:
<?php echo file_get_contents('path/to/mytextfile.txt'); ?>

You must use PHP for this to work, meaning:
1) page must end with .php (instead of .htm, and this won't hurt anything... works fine with html)
2) you need PHP installed/running on your server

Good luck.

CraigL
04-08-2007, 10:25 PM
Thank you djr33.

I will look into PHP. I don't know if my ISP has the server components for PHP installed.

I was hoping for a javascript, as I am a little more famaliar with javascripts.

-CL

djr33
04-09-2007, 01:25 AM
It is much better to use PHP for this and allows more control. (ASP, SSI [serverside includes], and other methods would work as well. PHP is just one serverside language.)

If you must go with Javascript, I believe you would need to use Ajax, which is much more complex and really shouldn't be needed for this. It's possible, though.

thetestingsite
04-11-2007, 03:19 AM
If you must go with Javascript, I believe you would need to use Ajax

Either way, you would need to use some sort of server side invocation for the AJAX script to work properly (unless you used Active X Controls - which is only available in IE).

Hope this helps.

EDIT: Misread the question. Thought you were trying to edit/write to a text file.

djr33
04-11-2007, 05:16 AM
Ajax can call a .php page; can it not call a .txt 'page'?

tech_support
04-11-2007, 05:32 AM
Ajax can call a .txt document.

And I've never seen a web host that'll allow CGI but not PHP.

djr33
04-12-2007, 05:34 AM
But it might have ASP, just CGI, etc. available/installed and not PHP, with that either needing to be installed, or just not available with that webhosting company.

thetestingsite
04-12-2007, 05:37 AM
I've never seen a web host that'll allow CGI but not PHP.

I have seen several that only allow perl scripts, and nothing more. I have also seen some that only have support for PHP without database support (if they needed it). It really all depends on the webhosting company (and/or the servers in which the user hosts the site on).