【问题标题】:Including .html file from parent dir fails to load .html assets包含来自父目录的 .html 文件无法加载 .html 资产
【发布时间】:2017-02-24 10:42:56
【问题描述】:

我正在尝试将 .html 包含到我的 .php 文件中。我可以这样做,但是相对于 .html 定义的资产似乎没有加载。

我试过了:

include(dirname(dirname( __FILE__)) . '/html/index.html');

PHP文件结构:

/misc/templates/template.php

HTML 文件结构:

/misc/html/index.html

.html 中的资产是相对于 .html 加载的,例如:

<link rel="stylesheet" href="assets/css/custom.css" />
//= /misc/html/assets/css/custom.css

如何在我的 template.php 文件中包含/加载 index.html,让 index.html 加载它的相关资产?

ps。纯粹使用 html iframe 嵌入不是一种选择

ps。将 .html 作为 .php 提供服务不是一种选择

【问题讨论】:

  • 为什么不使用 .php 作为 html 文件?你的问题就解决了。
  • 抱歉忘了提——由于各种原因,这也不是一个选择:) ...即使我将 index.html 重命名为 index.php,这会改变什么吗?资产仍然无法加载上述包含?
  • 您是否尝试在 HTML 文件中写入一些文本以查看您的 href 链接 okej?我正在我的服务器上尝试它的工作。
  • 文本和 HTML 包含很好 - 但是该 html 中的 CSS/JS 资产是相对于 html 文件并且没有加载
  • 你能提供“assets/css/custom.css”的路径吗?

标签: php include include-path dirname


【解决方案1】:

你需要为你的 CSS 文件提供完整的 url,因为 PHP 无法读取你当前的地址。

变化:

<link rel="stylesheet" href="assets/css/custom.css" />

改为:

<link rel="stylesheet" href="../html/assets/css/custom.css" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-25
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 2018-05-11
    • 2010-09-23
    • 1970-01-01
    相关资源
    最近更新 更多