php 如何在php代码中嵌入html文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12294120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:16:39  来源:igfitidea点击:

how to embed html files in php code?

phphtml

提问by sp2

I have many html files and now i want to call each of the files one by one using some php code. but whenever i try to run the php code for calling those html files from the folder, it doesnot work.

我有很多 html 文件,现在我想使用一些 php 代码一个一个地调用每个文件。但是每当我尝试运行 php 代码以从文件夹中调用这些 html 文件时,它都不起作用。

    1.html view
    2.html view
    3.html view

So, 1,2 and 3 are the html files and now by clicking view user should be directed to the link. How can i solve the problem for incorporating html files in php, so that the files will be displayed to user one by one with a view option. And whenever user will click view, user will get the html page viewed.

因此,1,2 和 3 是 html 文件,现在通过单击查看用户应该被定向到链接。如何解决在php中合并html文件的问题,这样文件会一一显示给用户,并带有查看选项。每当用户单击查看时,用户都会查看 html 页面。

Note: i m using xampp server in windows7

注意:我在 windows7 中使用 xampp 服务器

Any suggestions will be appreciated...

任何建议将不胜感激...

回答by Bashevis

Use the include PHP functionfor each html file.

对每个 html 文件使用包含 PHP 函数

example:

例子:

<?php
// do php stuff

include('fileOne.html');
include('fileTwo.html');

?>

回答by Kip

Small improvement to the accepted answer: you should prefer readfile()over include()or require()for non-PHP files. This is because include()and require()will be processed as PHP code, so if they contain something that looks like PHP (like <?), they may generate errors. Or worse yet, a security vulnerability. (There is still potential security vulnerability if you are displaying user-provided HTML too, of course, but they at least won't be able to run code on your server.)

小的改进,以接受的答案:你应该更喜欢readfile()include()require()用于非PHP文件。这是因为include()require()将作为 PHP 代码进行处理,因此如果它们包含看起来像 PHP(如<?)的内容,它们可能会产生错误。或者更糟糕的是,一个安全漏洞。(当然,如果您也显示用户提供的 HTML,那么仍​​然存在潜在的安全漏洞,但它们至少无法在您的服务器上运行代码。)

If the .htmlfiles actually contain PHP code, you should name them appropriately.

如果.html文件实际上包含 PHP 代码,您应该适当地命名它们。

<?php
// do php stuff

readfile('fileOne.html');
readfile('fileTwo.html');

?>

回答by Ron van der Heijden

Using opendirand loop throught the files.

使用opendir并循环遍历文件。

Example:

例子:

<?php

    $dir = "/tmp";
    $dh = opendir($dir);
    while(false !== ($filename = readdir($dh)))
    {
        echo $filename . ' <a href="' . $dir . '/' . $filename . '">view</a>';
    }
?>

回答by Sam Janssens

see here Loop code for each file in a directory

参见此处目录中每个文件的循环代码

to know how to loop the files in the folder, then just echo an A tag with a link

要知道如何循环文件夹中的文件,然后只需回显带有链接的 A 标记