【问题标题】:How to open a local file/url and include its output into another page via PHP如何通过 PHP 打开本地文件/url 并将其输出包含到另一个页面中
【发布时间】:2020-08-10 06:37:21
【问题描述】:

您好,我有一个单页网站(整个网站在一页中):

/index.html

我想将该页面的页眉和页脚包裹在服务器上文件夹内的另一个页面周围:

/folder/newpage.php

(带有/index.html的页眉和页脚,但内容不同)

我已经隔离了页眉和页脚,并将它们保存在根目录中的单独文件中:

/index_header.html
/index_footer.html

如果从根目录启动,它们会正确显示,但我似乎无法将它们包含在“newpage.php”中,因为没有加载页眉和页脚内的 CSS、JS 和图像。

我尝试了以下4种方法:

include($_SERVER["DOCUMENT_ROOT"].'/index_header.html');

file_get_contents($_SERVER["DOCUMENT_ROOT"].'/index_header.html');

ob_start();
include($_SERVER["DOCUMENT_ROOT"].'/index_header.html');
$output = ob_get_clean();
echo $output;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://".$_SERVER['HTTP_HOST']."/index_header.html");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
echo $output;
curl_close($ch);

但是这些方法都不起作用... 谁能帮我吗? 谢谢

【问题讨论】:

  • 检查readfile()
  • 你在哪里链接到 css、js 和图像?该代码在 index.html 页面上吗?
  • 也许这太容易解决了,但您是否尝试将 index.html 重命名为 index.php?大多数服务器不会执行 html 文件中的 php 代码。除此之外,我在我的简单网站中使用include 'my_file.hmtl,它工作正常。
  • @dalelandry 正确... index.html 是带有 CSS、JS 和图像的常规 html 文件
  • @MarkusZeller 我试过了,它的行为似乎和 include() 一样

标签: php html curl include file-get-contents


【解决方案1】:

我最终将所有 JS、图像和 CSS 调用更改为从根目录开始的绝对 url,这允许我使用 include() 谢谢

【讨论】:

    猜你喜欢
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    相关资源
    最近更新 更多