PDA

View Full Version : include file??


abrie
01-22-2005, 05:46 PM
Ok I want to add content to my site from an external text or html file.
See I'm using CSS to do all my menu's by jazzing up a list of links. So I have to put the link list in divs on EVERY page. Now I'm wondering (more like hope [really hard]) that I can use some kind of command that lets you include text into your html from an external file. So you put a link to it on your page and then it imports the content of the linked file into the html of your page.
Am I making sense? I hope so.
;)

Silver
01-22-2005, 06:13 PM
Well, you can use languages, like php to do stuff like this for u. Thats probably the most common method. And I'm sure that some1 or the other will give u a complete code if u want that method ;) . If they dont, and u want it, just ask.
Nyways, there is another method-> SSI-> Server Side Includes. In most places of webhosting, in cpanel, there is a option to enable or disable SSI.
Remember, that normally, this SSI power is still in protected mode when activated with cpanel. I'm quite sure to get full power, you need to ask the admin to set it up for u. Power mode is where commands like exec work as well.
Nyways, back to the topic (deviant, aint I), this SSI should work.
When you wanna consider input some info about the weather. You can just say-

<td><!--- Server side include is below --->
<!--#include file="weather.txt"-->

To specify a virtual parameter, ie, the location relative to the Web Server document root directory, you can replace the above with-

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


Try it, k. I'm not sure abt SSI here at HM. But this should work.

Dan
01-22-2005, 07:48 PM
I think that PHP would be the easiest way.

<?php include("file"); ?>

Just put that in your html code and you should be on your way. It's not very hard.
I don't know much about SSI but that looks way more complicated so I would recommend just using this. Hope it works for you.

if you want a little more indepth here's really nice site:
http://www.tizag.com/phpT/include.php

Very simple, if you have any questions just ask

Tim
01-22-2005, 08:50 PM
If you use Ic3CoLd's method, you also have to remember to have the file extension as ".php" You don't have to change around your file or anything, just change the extension by renaming it.

Silver
01-23-2005, 06:43 AM
As I am just jobless, and just feel like, here are some ways to include files (excluding SSI, which I already mentioned)-
--- --- --- --- ---
Using php (you can use readfile() and file() as well. This one is with fopen)

<?
$handle = fopen("/includes/text.txt", "r");
while (!feof($handle)) {
$buffer = fgets($handle, 4096); //4096 is the bytes stream. Defines how much to read into buffer at one go. You can increase it. Doing so will store more data in $buffer at once, using a bit more memory. Reducing it will increase the trips to the HD to get the file's content one by one.
echo $buffer;
}
fclose($handle);
?>

METHOD 2:

<?
$lines = file('/include/text.txt');
foreach ($lines as $line_num=>$line) {
echo $line;
}
?>


Include method of files is supposedly comparatively more dangerous to use, as it opens up a risk of security problems. The require() is also quite the same really. The main reason of the fault maybe cause the PHP code contained in any file they refer to is executed with PHP's privledges. Hence, by changing the file contents of the include file, through a backdoor, its very possible to cause serious injury to the system.
readfile(),file(),fopen()/fread() and most of such file reading functions just read and output the file as-is in the file, ie, no executions needed.

--- --- --- --- ---
Using PERL-
You gotto just save the file as a .pl instead of .html in this case. Same as that in php, where u save file as .php instead of .html

print "Content-Type: text/html\n\n";
#Using double quotes with EOF to allow variable interpolation
print<<"EOF";
<html><head>
....
etc etc etc.
blah blah blah

EOF
#Above shows end of printing.
#Now, to open and include the inclusive file.
open(INFILE,"<text.txt");
@file_contents=<INFILE>;
close(INFILE);
foreach $line(@file_contents){
print "$line";
}
print<<"EOF";
and here is all the other stuff u wanna put.
...

This is ofcourse a bit bulky.
There is another way of using sysread() and read() to get the files. Alot faster, but far more bulkier, and not recommended if you dont have experience with streams.
A way to do the same thing above, in a bit of PERL oop can be like-

use IO::Handle
print "Content-type: text/html\n\n";
print<<"EOF";
<html>
blah blah blah
...
EOF
$fh=new IO::Handle;
$fh->open("C:\file\text.txt");
$fh->fdopen($fh,"r");
if($fh->opened){
@lines=$fh->getlines;
print @lines;
}
print<<"EOF";
etc etc etc


Any1 want the same thing with C?

#include<stdio.h>
void main(){
FILE *fp;
fp=fopen("text.txt","r");
printf("Content-type:text/html\n\n");
printf("All stuff here.");
if(fp==NULL){ print("cant print file"); exit(); }
while(1){
char ch=getch(fp);
printf("%c",ch);
}
fclose(fp);
}


There, that should do nicely if any of the already given methods don't work. If these dont work either, there's always C++ (;) ).

abrie
01-23-2005, 08:46 AM
wow, thanks for all the help guys, I'm sure one of those methods will work. If I use SSI, do I have to change the file extension to something other than .html?

Anyways, thanks guys, I think I'll try the SSI first and if I have problems with that I'll do php.

abrie
01-23-2005, 09:32 AM
I tried Silvers SSI, and it worked, the only thing was that i had to add these three lines to my .htaccess file:

Options Indexes FollowSymLinks Includes
AddType application/x-httpd-cgi .cgi
AddType text/x-server-parsed-html .html

then i put the ssi code in and it works. THanks man!

Canada1824
01-25-2005, 09:58 PM
The info up there is good

but what if you are using frontpage? I tried everything. What should I do?

shwaza
01-25-2005, 10:11 PM
There shouldn't be any difference in frontpage... It's still the same old html... Which one are you using? The php one? Or silvers really long one lol.

Silver
01-26-2005, 07:06 AM
In frontpage, no. There is no way I am aware of, to include files.
shwaza, when u use SSI, php, PERL, or nything like that, you are using server side inclusion/execution . Standalone, inclusion of files cannot be done.
Hence, just adding the codes above, into the frontpage document is absolutely no use. You need to run pipes from a web server to the interpreter needed, so that the inclusion occurs.
I am quite unaware of including files with pure HTML.