Web Hosting Talk







View Full Version : PHP Import HTML external file


osphere
04-27-2006, 10:35 PM
Hi, I have a problem that I can't solve, I've searched in google, in php documentation and nothing...

I have a PHP page, and I need to import plain HTML code into the page that contains the PHP code.

The file that contains the HTML is located in the same folder...

Please someone know how to do this?

:)

RWJDCom
04-27-2006, 10:39 PM
Use somthing like...


$fd = fopen("file.html", r);
$content = fread($fd, filesize("file.html"));
fclose($fd);

echo $content;


Wherever you echo the $content your HTML file will be displayed. If you need further assistance feel free to PM me.

osphere
04-27-2006, 10:53 PM
Thanks man, it works perfectly, Thanks!

Saeven
04-27-2006, 11:12 PM
Even simpler:


echo file_get_contents( "file.html" );

bear
04-28-2006, 06:56 AM
Why not this?
include('file.html');

El-Vino
04-28-2006, 09:44 AM
Why not this?
include('file.html');
or
require('file.html');
if the file is mandatory and you want the loading to die if it's not found...

osphere
04-28-2006, 09:51 AM
or
require('file.html');
if the file is mandatory and you want the loading to die if it's not found...

Thanks to all of you guy's, Think I need to read again PHP for dummies...

Thanks :)

mikey1090
04-28-2006, 10:07 AM
i use require "file.php";

atomicshockwave
05-05-2006, 09:41 AM
This is like what I have ben looking to do, only is there a way to do this if there are meany html files, and diffrent names where you dont know the names?
odd ??? I know but if anyone could help

TwistMyArm
05-05-2006, 10:32 AM
You can always use your include in a loop, with a variable.

Do something like open the directory and loop through all of the files. For each file, if they match a given string template, include that file.

Should be pretty simple.

nnormal
05-05-2006, 10:50 AM
no need for a loop...


<?
if ((isset($_GET['p'])) && (isfile($_GET['p'].'.html'))) include($_GET['p'].'.html');
else include('default.html');
?>


you would call pages with index.php?p=mypage

atomicshockwave
05-05-2006, 11:20 AM
I am a little new to php, could you explain how your code would work nnormal?
just looking at it, it looks like this still needs a file that you know the name of the file. also would this alow me to do this 10+ times in one page, and could I put this php script in a html page?

sorry for all the ????, like I said I am a little new to php. thanks

TwistMyArm
05-05-2006, 11:22 AM
Absolutely. I just thought that atomicshockwave was talking about including multiple HTML files with unknown names into one PHP script.

nnormal
05-05-2006, 11:25 AM
it wouldnt put multiple files in for that you may need a loop. are you trying to include each file from a directory or something?


<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
include($file);
}
}
closedir($handle);
}
?>

atomicshockwave
05-05-2006, 11:41 AM
I need to have dhtml popup window for each html file in a folder on my site, and I need the script to add the window script, because files are added and removed evry day, now my plain is that in the html files would be the window script, and the php code would find the files and run the code in the index.html file (my homepage where I need the windows.

Saeven
05-05-2006, 02:57 PM
Never EVER use $_GET in conjunction with file commands directly:

isfile($_GET['p'].'.html')

This can be abused by the most unknowledgeable hacker. While I won't get into proper filtering techniques, novices could use a switch statement:


switch( $_GET['p'] ){
case 'file1':
include( 'file1.html');
break;
//...

default:
include( 'default.html' );
}



}

gedevelopment
05-07-2006, 06:04 AM
use the include() function

atomicshockwave
05-07-2006, 06:22 AM
never heard of the include() function gedevelopment (http://www.webhostingtalk.com/member.php?u=145628)
could you give me some more info in that function? vbmenu_register("postmenu_3851379", true);

gedevelopment
05-07-2006, 06:27 AM
ok, lets say you have a 1 page site. you have a php file and then use a html template to make life easier to edit the page visually. (in say frontpage etc)

you would use the include() to put that html code into the page and display it.

example:

HTML TEMPLATE FILE: template.html

<b> Hello And Welcome To This Test </b>

PHP FILE: index.php


<html>
<head>
<title>Some Title</title>
</head>

<body>
<?php
include ("template.html");
?>
</body>
</html>

This will display: Hello And Welcome To This Test when the index.php file is loaded. Basically it put the HTML code into the php code. Makes visual editing of pages alot easier.

atomicshockwave
05-07-2006, 06:48 AM
thanks gedevelopment!
I like your idea to use the include()
and using that basic idea I found this on the web, it needs some adjustment, but I think it just might work:
linker.php ( this page is to autolink textfiles in your directory)
-------------
<table border="1" bordercolor="#000000" align="center" WIDTH="75%"><tr
bgcolor="#999999"><td><font
color="#000000">Name</font></td><td><font color="#000000">Size</font></td></tr>

<?php

exec (http://www.zend.com/manual/function.exec.php)("ls -la $dir",$lines,$rc);
$count = count (http://www.zend.com/manual/function.count.php)($lines) - 1;
for ($i = 1; $i <= $count; $i++) {
$type = substr (http://www.zend.com/manual/function.substr.php)($lines[$i],0,1);
$name = strrchr (http://www.zend.com/manual/function.strrchr.php)($lines[$i]," ");
$name = substr (http://www.zend.com/manual/function.substr.php)($name,1);
$dire = substr (http://www.zend.com/manual/function.substr.php)($lines[$i],0,strpos (http://www.zend.com/manual/function.strpos.php)($lines[$i],$name));

// echo "$dire</font>"; //
if ($type == "d") {
if ($name == "." or $name == "..") {
;
} else {
if ($dir == "") {
$size=filesize (http://www.zend.com/manual/function.filesize.php)($name);
echo "<tr bgcolor="#999999"><td><a href="$name">$name</a></td><td>$size</td></tr>";
} else {
$size=filesize (http://www.zend.com/manual/function.filesize.php)($name);
echo "<tr bgcolor="#DDDDDD"><td><a href=$dir/disp?i=$name>$name</a></td><td>$size</td></tr>";
}
}
} else {
if ($dir == "") {
$size=filesize (http://www.zend.com/manual/function.filesize.php)($name);
echo "<tr bgcolor="#999999"><td><a href=disp.php?i=$name>$name</a></td><td>$size</td></tr>";
} else {
echo "<tr bgcolor="#DDDDDD"><td><a href=$dir/?i=$name>$name</a></td></tr>";
}
}
}
?>

</table><br>

disp.php (this page is to display the textfiles in tableized manner)
-----------------
<?php

$text_dir="/usr/home/jiwang/public_html/test"; //the file path of your textfile
$text_url="http://www.jiwang.org/test"; //this is the url of your directory

//change this below if you so desire...
?>

<center><table BORDER CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="75%" BGCOLOR="#999999"
bordercolor="#000000">
<tr>
<td><pre>
<?php

//of course you can add errors to this script. this will display them...
function error($what) {
echo "<u>doc error...</u><p>n";
echo $what;
exit;
}

if(!$i) {
echo "<title>docs</title>n</head><table><form action=$PHP_SELF><tr><td valign=top>docs: <input type=text name=i></td><td valign=top><input type=submit value=submit></form></td></tr></table>n";
exit;
}

else {$i=addslashes (http://www.zend.com/manual/function.addslashes.php)($i); echo "<title>documents - $i</title>n</head>n";}

$file=$text_dir."/".$i; $url=$text_url."/".$i;

if(is_file (http://www.zend.com/manual/function.is-file.php)($file)) {

include ("$file");

}

else {error('file does not exist');}

?>

</pre>
</td>
</tr>
</table></center>

El-Vino
05-07-2006, 12:18 PM
On the case of a absolutely necessary inclusion, like the one described by gedevelopment, a require() is a lot more adapted as the page would have no logical content without the html file.