I don't know the proper term for this, but here is what I want to do: read data from an external .txt file or .doc file into html page. If the file is changed and re-saved, the website automatically changes as well. I know that css style sheets are similar to this - you load your settings into a css file and if you make a change, the website updates accordingly. But a css file is just for visual stlyes. I actually want the "words" from the file to be displayed and updated accordingly. If you understand what I mean, please advise how to do this. Thanks :)

Recommended Answers

All 11 Replies

For as far as I know, there is no easy fix for this problem. I have seen a solution with javascript some time ago, but mostly it requires the use of php.

Maybe, if your site is hosted on an Apache server, you could use an Apache directive: <!--#include virtual="mytext.txt"-->
If the Apache configuration allows this, the file mytext.txt will be read in and replace the directive. But even if this works, you have to be very careful with plain text files. It can make a mess of the layout of your page in no-time.

Don't know if it can be done with ASP from a Microsoft webserver. My know how on ASP is almost zero, sorry.

Thanks colweb. I do have an Apache server, and I tried the directive, but it didn't work. I am new to Apache server so i may not have it configured correctly to display the directive. I don't want anything too complicated, because I am working on a website for someone, and I want them to be able to update certain content themselves without having to mess with html and such. I thought I saw in the past somewhere that certain content could be updated with just a text file. Don't remember where I saw this. If it is not something easy then I will try another approach.

I manage my own Apache server and have a virtual site where I allow includes. The configuration for this (in httpd.conf) is as follows:

# -------------------------------------------------
    # vvvnew.colhome.net
    # -------------------------------------------------
    <VirtualHost *:80>
	DocumentRoot "/var/www/vvvnew/htdocs"
	ServerName vvvnew.colhome.net
	ErrorLog /var/log/httpd/vvvnew/error_log
	CustomLog /var/log/httpd/vvvnew/access_log combined
	<Directory "/var/www/vvvnew/htdocs">
	    Options +Includes
	    AddType text/html .shtml
	    AddOutputFilter INCLUDES .shtml
	    AddOutputFilter INCLUDES .html
	    Order allow,deny
	    Allow from all
	</Directory>
    </VirtualHost>

In the global section of the httpd.conf file you must not forget to load mod_include:

LoadModule include_module /usr/lib/httpd/modules/mod_include.so

It should also be possible to place the include directive inside the .htaccess file, but if you can modify the httpd.conf that is the preferred way to do it.
Then, in the html file:

<body>
  <p>
  <!--#include virtual="somefile"-->
  </p>
</body>

This action will insert the file "somefile" in your html page between the <p> tags. I use this all the time with this site to include the menu, a flash part, the header and footer.
You can insert html, javascript, php-code and plain text files. Doc files contain a binary part (try opening it with notepad under windows), so to insert those, you will need php or javascript.

I manage my own Apache server and have a virtual site where I allow includes. The configuration for this (in httpd.conf) is as follows:

# -------------------------------------------------
    # vvvnew.colhome.net
    # -------------------------------------------------
    <VirtualHost *:80>
	DocumentRoot "/var/www/vvvnew/htdocs"
	ServerName vvvnew.colhome.net
	ErrorLog /var/log/httpd/vvvnew/error_log
	CustomLog /var/log/httpd/vvvnew/access_log combined
	<Directory "/var/www/vvvnew/htdocs">
	    Options +Includes
	    AddType text/html .shtml
	    AddOutputFilter INCLUDES .shtml
	    AddOutputFilter INCLUDES .html
	    Order allow,deny
	    Allow from all
	</Directory>
    </VirtualHost>

In the global section of the httpd.conf file you must not forget to load mod_include:

LoadModule include_module /usr/lib/httpd/modules/mod_include.so

It should also be possible to place the include directive inside the .htaccess file, but if you can modify the httpd.conf that is the preferred way to do it.
Then, in the html file:

<body>
  <p>
  <!--#include virtual="somefile"-->
  </p>
</body>

This action will insert the file "somefile" in your html page between the <p> tags. I use this all the time with this site to include the menu, a flash part, the header and footer.
You can insert html, javascript, php-code and plain text files. Doc files contain a binary part (try opening it with notepad under windows), so to insert those, you will need php or javascript.

Thanks for this info. I am running Apache with WinXP Pro SP3. How would I change the httpd.conf info to achieve same results? You are apparently using linux, so your directory structure is different. Sorry if I sound stupid, but I was using IIS5.1 before and my php was unstable with it, so I installed apache. Now there are things with apache that are different (although in a good way).

Well there is not much different when running Apache on Windows. The paths are different, so on Linux you use:

LoadModule include_module /usr/lib/httpd/modules/mod_include.so

and for Windows:

LoadModule include_module modules/mod_include.so

Then inside the <Directory></Directory> where your site is parked you add:

Options +Includes
	    AddType text/html .shtml
	    AddOutputFilter INCLUDES .shtml
	    AddOutputFilter INCLUDES .html

It's really not difficult. Maybe you could also look here for more info: http://httpd.apache.org/docs/2.0/howto/ssi.html

Here are examples from my httpd.conf file:

The include module is loaded.....

#LoadModule ident_module modules/mod_ident.so
#LoadModule imagemap_module modules/mod_imagemap.so
[B]LoadModule include_module modules/mod_include.so[/B]
#LoadModule info_module modules/mod_info.so
LoadModule isapi_module modules/mod_isapi.so
#LoadModule ldap_module modules/mod_ldap.so

The "Directory" info as it was.....

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
</Directory>

The "Directory" info after making changes.

<Directory />
Options +Includes
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
    AddOutputFilter INCLUDES .html
    Order allow,deny
    Allow from all
    
Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
</Directory>

******************
I put the tag into my page as follows:

<!--#include virtual="/test.txt"-->

And stopped/started apache, but I still get nothing to show up
What am I doing wrong?

Member Avatar for ingeva

This is an example of how to make very simple tasks incredibly complicated.

With php, just do:

<?php include ("textfile.txt"); ?>

and you're done.

I usually write most of my websites as php so they can use variables, make decisions etc.

Take some time to learn php. You'll save much more time later.

I have downloaded "apache_2.0.63-win32-x86-openssl-0.9.7m.msi" from the Apache site and installed in under WinXP SP1.

The top of the httpd.conf file looks now looks like this:

PidFile logs/httpd.pid
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 15
<IfModule mpm_winnt.c>
	ThreadsPerChild 250
	MaxRequestsPerChild  0
</IfModule>
Listen 8080
LoadModule access_module modules/mod_access.so
LoadModule actions_module modules/mod_actions.so
LoadModule alias_module modules/mod_alias.so
LoadModule asis_module modules/mod_asis.so
LoadModule auth_module modules/mod_auth.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule cgi_module modules/mod_cgi.so
LoadModule dir_module modules/mod_dir.so
LoadModule env_module modules/mod_env.so
LoadModule imap_module modules/mod_imap.so
LoadModule include_module modules/mod_include.so
LoadModule isapi_module modules/mod_isapi.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module modules/mod_mime.so
LoadModule negotiation_module modules/mod_negotiation.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule userdir_module modules/mod_userdir.so
ServerAdmin admin@colhome.net
ServerName winxp2.colhome.tst:8080
UseCanonicalName Off
DocumentRoot "C:/Program Files/Apache Group/Apache2/htdocs"
<Directory />
	Options FollowSymLinks
	AllowOverride None
</Directory>
<Directory "C:/Program Files/Apache Group/Apache2/htdocs">
	Options Indexes FollowSymLinks
	AllowOverride None
	Order allow,deny
	Allow from all
	Options Includes
	AddType text/html .shtml
	AddHandler server-parsed .shtml .html
 	AddOutputFilter INCLUDES .shtml .html .htm .asp
</Directory>

After this a couple of 100 lines follow...

Then I created a html file and a text file with only the words "text" in it. The html file looks like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
  <p>
  <!--#include virtual="/test.txt"-->
  </p>
</body>
</html>

Started the Apache server, started Firefox 2 and went to http://localhost:8080/test.html. You can see in the config file that the server port is 8080. And you know what? The include works :) :)
It seems that the line "AddHandler server-parsed .shtml .html" does the trick to get it working under Windows. Without that line I did't get it working no matter what I tried. Maybe there is more difference between the Apache for Windows and Apache for Linux than I was aware :-O

Looking at your config file it seems you added the include directive at the wrong place. You have to look for something like <Directory "C:/...> or create something like that, and place the include directive here.

Anyway, hope this will help.

Your httpd.conf file looks different than mine. Maybe it's because you are using Apache 2.0.63 and I am using Apache 2.2.10. I don't know what would happen if I changed areas of my httpd.conf file to look like yours. Thanks for your help, but maybe ingeva is right (see below) - php would be much simpler.

This is an example of how to make very simple tasks incredibly complicated.

With php, just do:

<?php include ("textfile.txt"); ?>

and you're done.

I usually write most of my websites as php so they can use variables, make decisions etc.

Take some time to learn php. You'll save much more time later.

Yes the http.conf shouldn't have to be modified for simple read and write of text files. The http.conf is more used for things like enabling advanced functions like mod_rewrite that aren't normally enabled by default.
Look into fopen, fwrite, and fread. Also if you run into trouble, DaniWeb has one of the best php forums in the world, hands down.

Member Avatar for ingeva

Thanks for your help, but maybe ingeva is right (see below) - php would be much simpler.

I would like to correct myself a little, and expand a little too.

If you "include" a file with php, it's usually another php or html file which is processed by php to create the output.

However, if you have a file with pure html content, you could use
readfile instead:

<?php readfile ("text.file"); ?>

I often use a variable so the script can select from several files. In this case, the variable "$Action" contains the first name of the file to be included:

<?php include ("$Action.php"); ?>

A nice way that php can be used is:

<?php include ("MyCSSfile.php"); ?>

which is a CSS file that can contain php variables. Nice for defining some variations, like random color selections, background images etc.

If you make web content, you should learn php (Or perl, but I prefer php). Simple as that. There are several tutorials and php forums around the Net.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.