【问题标题】:setting the absolute path for pages设置页面的绝对路径
【发布时间】:2012-09-08 21:19:45
【问题描述】:

我一直在努力改善我的网站结构,以使其不那么混乱,但我遇到了文件路径问题。

网站结构之前

[folder] :   css
[folder] :   js
[folder] :   images
[folder] :   include
   |---> :   header.php
[file]   :   index.php
[file]   :   view_inventory.php
....

之后的网站结构

[folder] :   css
[folder] :   js
[folder] :   images
[folder] :   include
   |---> :   header.php
[folder] :   view
   |---> :   inventory.php
[file]   :   index.php
....

如您所见,我决定将视图文件分组,将它们放入自己的文件夹中

问题在于 index.phpview_inventory.php 都使用 include/header.php 文件

include/header.php文件有一个标题标志位于images/logo.php,如果两个图像的路径不会有问题index.phpview_inventory.php 在同一个文件夹中,但由于它们不是 view/inventory.php 中的文件,因此图像不正确路径。

我曾考虑为图像等文件定义完整路径(例如 localhost/sites/bk/images/logo.png),但这不是一个好主意,因为我可能决定将文件上传到服务器和服务器地址可以不断变化..等等

解决这个问题的最佳方法是什么?

【问题讨论】:

    标签: php html apache path


    【解决方案1】:

    使用绝对路径!

    在你的 index.php 中做:

    define( 'ROOT', dirname(__FILE__) );
    

    而当你想包含头文件时,

    include ROOT . '/include/header.php'
    

    并且,当在 html 中并想要使用图像时,请使用来自 webroot 的绝对路径:

    <img src="/images/your_image.png">
    

    【讨论】:

    • webroot 不能这样工作,当我需要文件位于 localhost/sites/bk/images/logo.png 时,它会转到 localhost/images/logo.png
    • 如果localhost/images/logo.png在浏览器中显示徽标,则将您的代码编写为 !
    • 我可以将根路径从 htdocs 更改为 htdocs/sites/bk/
    猜你喜欢
    • 2013-03-07
    • 2020-04-10
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多