doufu7835
2015-03-09 09:07
浏览 24
已采纳

如何在php中使用嵌套包含时访问所有文件

I am facing some trouble in accessing the file from a folder.

My current scenario is: I can accces following folders:

  • localhost/testproject/
  • localhost/testproject/inc
  • localhost/testproject/news

And I have two files:

  • localhost/testproject/inc/header.php
  • localhost/testproject/news/test.php

In the header.php there are some css files included. Now the problem is that if I try to include the header.php in test.php it does not load the css files.

图片转代码服务由CSDN问答提供 功能建议

我在从文件夹访问文件时遇到一些麻烦。

我目前的情况是: 我可以使用以下文件夹:

  • localhost / testproject /
  • localhost / testproject / inc
  • localhost / testproject / news

    我 有两个文件:

    • localhost / testproject / inc / header.php
    • localhost / testproject / news / test.php

      在header.php中包含了一些css文件。 现在问题是,如果我尝试 在 test.php 中包含 header.php ,它不会加载css文件。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drf16571 2015-03-09 09:43
    已采纳

    I find that out that I have to create a site url in config file and include all the css and the js files according to that site url.

    config.php
    
    define("SITE_URL",'http://localhost/testproject/');
    
    header.php
    
    include "config.php";
    
    <link href="<?php echo SITE_URL; ?>css/styles.css" rel="stylesheet">
    

    And that is it :) thanks every one for there response.

    打赏 评论
  • dongshou1856 2015-03-09 09:29

    If I understand correctly, you have the CSS link in the header.php. The best solution is to change the link from

    <link rel="stylesheet" href="style_of_header.css" type="text/css">

    to:

    <link rel="stylesheet" href="./inc/style_of_header.css" type="text/css">

    The . takes you back to the root folder. From there you can specify the folder relative from the root folder.

    打赏 评论

相关推荐 更多相似问题