如何打开本地文件/ URL并通过PHP将其输出包含到另一个页面中

问题描述 投票:0回答:1

您好,我有一个网站页面(整个网站在一页中:]

/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);

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

php html curl include file-get-contents
1个回答
0
投票

我最终将所有JS,图像和CSS调用从根目录更改为绝对URL,这使我可以使用include()谢谢

© www.soinside.com 2019 - 2024. All rights reserved.