【问题标题】:Include files from different folder PHP [duplicate]包括来自不同文件夹PHP的文件[重复]
【发布时间】:2015-12-14 18:55:35
【问题描述】:

我知道这个问题已经被问过很多次了,但我找不到任何正确的答案来解决我的问题。

我的文件夹结构是:

 ├── index.php
 ├── js/
 │   └── js.js
 ├── css/
 │   └── style.css
 ├── src/
 │   └── include.php
 └── level1/
     └── index.php

在include.php中:

<script src="js/jquery-1.11.0.min.js"></script>
<link href="css/style.css" type="text/css" rel="stylesheet" />

在 index.php 中:

<?php include('src/include.php');?> // works fine

level1/index.php中,我调用了

<?php include('../src/include.php');?> // js and css file included but points to level1 folder.

我想将 js & css 样式文件分别保存在 js & css 文件夹中,并且仍然能够从不同的文件夹中包含它们。

【问题讨论】:

  • 我不确定我是否理解这个问题?
  • src src="/js/jquery-1.11.0.min.js" 之前添加一个斜线,同样适用于您的样式表
  • 它指向主目录,而不是应用程序目录
  • 然后src="/path/to/app/js/jquery-1.11.0.min.js"
  • 就像@roullie 所说,您必须为资产设置绝对 URL。当然,您必须将整个路径设置为:/path/to/my/public/assets/css/my-css.css。也为 js 设置这个

标签: php


【解决方案1】:

1.您必须了解php和css之间的“/”的区别。对于php,它以您系统的文件系统开头,但对于css/html/js,它表示您的webroot。

2.我使用这样的代码 在 include.php 上

define("TEMPLATE_PATH",dirname(__FILE__)."/template");//this is for PHP file's include
include TEMPLATE_PATHE."/test.php";//php file as an example

define("WEB_TEMPLATE_PATH","/rain/template");//this is for html
<script src="<?=WEB_TEMPLATE_PATH?>/js/jquery-1.11.0.min.js"></script>
<link href="<?=WEB_TEMPLATE_PATH?>/css/style.css" type="text/css" rel="stylesheet" />

【讨论】:

  • html 和 CSS 一起写的,有没有人能告诉我这个格式是否正确?
【解决方案2】:

您的问题是,&lt;script&gt;&lt;link&gt; 将使用相对于它们所在位置的目录,这意味着

js/jquery-1.11.0.min.js

在一种情况下,并且

level1/js/jquery-1.11.0.min.js

要解决此问题,如果您的目录结构位于根目录上,您可以在前面加上 /(如 @roullie 在 comments 中提到的),或者像 /se/appname/ 这样的完整路径。

您还可以提供一些返回(绝对)基本路径的全局实例,以防止拼写错误和类似错误。例如,请查看代码点火器base_url()here

【讨论】:

    【解决方案3】:
    On include.php ,filepath will different for both locations
    
    on index.php
    <script src="js/jquery-1.11.0.min.js"></script>
    <link href="css/style.css" type="text/css" rel="stylesheet" />
    
    because these file are including from index.php that is on root folder
    
    On level1/index.php, I called
    <script src="../js/jquery-1.11.0.min.js"></script>
    <link href="../css/style.css" type="text/css" rel="stylesheet" />
    because these file are including from level1/index.php that is in level1 folder
    

    【讨论】:

    • 这不是解决方案。它增加了不必要的冗余。包含可以消除这种情况。
    • 如果你想使用公共路径,那么你必须使用该文件的绝对路径
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    • 2012-02-18
    • 2012-09-03
    • 2016-05-23
    相关资源
    最近更新 更多