PHP -如何加载HTML文件?(复制)

时间:2023-01-24 11:28:05

This question already has an answer here:

这个问题已经有了答案:

At the moment I have a file like this

现在我有一个这样的文件

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    echo "<html>My HTML Code</html>";
}
?>

But I wanted to do something like this to keep my php file short and clean.

但是我想做这样的事情来保持php文件的简洁。

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    //print the code from ..html/myFile.html
}
?>

How can I achieve this?

我如何做到这一点?

8 个解决方案

#1


10  

you may have a look at PHP Simple HTML DOM Parser, seems a good idea for your needs! Example:

您可以看一下PHP简单的HTML DOM解析器,这似乎是满足您的需要的好主意!例子:

// Create a DOM object from a string
$html = str_get_html('<html><body>Hello!</body></html>');

// Create a DOM object from a URL
$html = file_get_html('http://www.google.com/');

// Create a DOM object from a HTML file
$html = file_get_html('test.htm');

#2


15  

save your html content as seperate template and simply include it

将html内容保存为seperate模板,并简单地包含它。

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    include ("your_file.html");
}
?>

OR

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    readfile("your_file.html");
}
?>

readfile is faster and less memory intensive than file_get_contents

readfile比file_get_contents速度更快,内存消耗也更少

#3


3  

Use this code

使用这个代码


if(some condition)
{
    //Dont allow access
}
else
{
    echo file_get_contents("your_file.html");
}

OR


if(some condition)
{
    //Dont allow access
}
else
{
    require_once("your_file.html");
}

#4


2  

Use functions like

使用函数如

include()
include_once()
require()
require_once()
file_get_contents()

#5


2  

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    echo file_get_contents("your_file.html");
}
?>

This should do the trick

这应该能达到目的

Or, as nauphal's answer say, simply use include()

或者,如nauphal的回答所说,简单地使用include()

Don't forget that, if file doesn't exists, you could have some trouble (so, maybe, check before include or getting content)

不要忘记,如果文件不存在,您可能会遇到一些麻烦(因此,可能在包含或获取内容之前检查)

#6


1  

I think you want to include your HTML file or have I misunderstood the question.

我想你想要包含你的HTML文件,或者我误解了这个问题。

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    include ("..html/myFile.html");
}
?>

#7


1  

Extending nauphal's answer for a more robust solution..

扩展nauphal的解决方案以获得更健壮的解决方案。

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    if(file_exists("your_file.html"))
    {
       include "your_file.html";
    }
    else
    {
      echo 'Opps! File not found. Please check the path again';
    }
}
?>

#8


-1  

Way 1:

方式1:

ob_start();
include "yourfile.html";
$return = ob_get_contents();
ob_clean();

echo $return;

Way 2: Use templaters, like CTPP, Smarty, etc... Templaters are useful to transfer some logic from php to template, for example, in CTPP:

方法二:使用模板,比如CTPP, Smarty等等…模板对于将一些逻辑从php传输到模板非常有用,例如,在CTPP中:

$Templater -> params('ok' => true);
$Template -> output('template.html');

in template html:

在html模板:

<TMPL_if (ok) >
ok is true
<TMPL_else>
ok not true
</TMPL_if>

The same ideas are in other templaters. Templaters are better, cause it helps you to standartize your templates and send all primitive logic to them.

其他的模板也有同样的想法。模板更好,因为它可以帮助您标准化模板并将所有原始逻辑发送给它们。

#1


10  

you may have a look at PHP Simple HTML DOM Parser, seems a good idea for your needs! Example:

您可以看一下PHP简单的HTML DOM解析器,这似乎是满足您的需要的好主意!例子:

// Create a DOM object from a string
$html = str_get_html('<html><body>Hello!</body></html>');

// Create a DOM object from a URL
$html = file_get_html('http://www.google.com/');

// Create a DOM object from a HTML file
$html = file_get_html('test.htm');

#2


15  

save your html content as seperate template and simply include it

将html内容保存为seperate模板,并简单地包含它。

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    include ("your_file.html");
}
?>

OR

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    readfile("your_file.html");
}
?>

readfile is faster and less memory intensive than file_get_contents

readfile比file_get_contents速度更快,内存消耗也更少

#3


3  

Use this code

使用这个代码


if(some condition)
{
    //Dont allow access
}
else
{
    echo file_get_contents("your_file.html");
}

OR


if(some condition)
{
    //Dont allow access
}
else
{
    require_once("your_file.html");
}

#4


2  

Use functions like

使用函数如

include()
include_once()
require()
require_once()
file_get_contents()

#5


2  

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    echo file_get_contents("your_file.html");
}
?>

This should do the trick

这应该能达到目的

Or, as nauphal's answer say, simply use include()

或者,如nauphal的回答所说,简单地使用include()

Don't forget that, if file doesn't exists, you could have some trouble (so, maybe, check before include or getting content)

不要忘记,如果文件不存在,您可能会遇到一些麻烦(因此,可能在包含或获取内容之前检查)

#6


1  

I think you want to include your HTML file or have I misunderstood the question.

我想你想要包含你的HTML文件,或者我误解了这个问题。

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    include ("..html/myFile.html");
}
?>

#7


1  

Extending nauphal's answer for a more robust solution..

扩展nauphal的解决方案以获得更健壮的解决方案。

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    if(file_exists("your_file.html"))
    {
       include "your_file.html";
    }
    else
    {
      echo 'Opps! File not found. Please check the path again';
    }
}
?>

#8


-1  

Way 1:

方式1:

ob_start();
include "yourfile.html";
$return = ob_get_contents();
ob_clean();

echo $return;

Way 2: Use templaters, like CTPP, Smarty, etc... Templaters are useful to transfer some logic from php to template, for example, in CTPP:

方法二:使用模板,比如CTPP, Smarty等等…模板对于将一些逻辑从php传输到模板非常有用,例如,在CTPP中:

$Templater -> params('ok' => true);
$Template -> output('template.html');

in template html:

在html模板:

<TMPL_if (ok) >
ok is true
<TMPL_else>
ok not true
</TMPL_if>

The same ideas are in other templaters. Templaters are better, cause it helps you to standartize your templates and send all primitive logic to them.

其他的模板也有同样的想法。模板更好,因为它可以帮助您标准化模板并将所有原始逻辑发送给它们。