-1

这是我面临的情况,以下是网站的结构:

 index.php
 /inc
    functions.php
 /css
 /img
 /connect
    db.class.php
    setup.php
 /admin
    index.php
    /img
    /css
    /inc
       functions.php
    /connect
       db.class.php
       setup.php 
    /content
       page.php
       /op
         add.php
         modify.php
         delete.php

我已经使用绝对和相对路径、DIR、$_SERVER['DOCUMENT_ROOT'] 等进行了测试

问题是当我尝试将文件夹 inc 中的 functions.php 文件包含到文件夹内容或操作中时。我已经以一些不同的方式进行了测试,并从这里检查了一些答案,我无法修复它。

有人可以解决这个问题吗?

4

3 回答 3

2

我有同样的问题,我在网上搜索了很多问题,但我没有找到任何问题,所以我自己做了,所以这里是:

define('GLOBAL_APP_ROOT', $_SERVER["DOCUMENT_ROOT"]); // you can append to this path the folder in which your site is deployed, in case you are working for a new version or something, and when you will deploy it you just delete it

public static function ToAbsolute($path)
{
    $root = GLOBAL_APP_ROOT;
    $cwd = getcwd(); // the current working directory => usually the directory from where the script its being called.
    $cwd = str_replace('\\', '/', $cwd); // sometimes the current working directory is exposed with '\' instead of '/' - this usually happens on localhost
    $path = ltrim($path,'/'); // cleans the left slash if exists => to avoid any writing errors/bugs

    $depthPath = str_replace($root, '', $cwd); // gets the depth path of the current file

    $depth = substr_count($depthPath, "/"); // gets the dept of the current working directory relative to document root

    $relativePrefix = ""; // declaration of the relative prefix path
    for($i = 0; $i < $depth; $i++) // building up the relative prefix
    {
        $relativePrefix = $relativePrefix."../"; // iterating the depth results in going back one folder
    }

    $relativePath = $relativePrefix.$path; // prepend the relative prefix to the absolute file path resulting in the relative path

    return $relativePath;
}

我把这个函数放在root了应用程序的url.php文件中。当您想使用它时,它将是这样的:

<?php
    require_once('url.php');
    require_once(ToAbsolute('[file_path]'));
?>

因此,始终url.php在您想使用它的任何地方包含第一个,并且始终include将它与它的relative path. include您只需要使用该函数并传入的absolute path所有其他文件file

笔记:

例如,如果您具有以下结构:

[root]
    [admin]
        [index.php]
    [url.php]
    [css]
        [site.css]

在里面index.php你必须有这样的东西来包括site.css

<?php
    require_once('../url.php');
    require_once(ToAbsolute('site.css'));
?>
于 2013-09-15T05:23:01.583 回答
0

我认为你在路上错过了一个../,即

您必须使用以下代码-

include('../../../inc/functions.php')如果要将文件包含在文件function.php夹中的任何文件中并将其包含在文件夹/op中的文件中,/content则必须使用以下一个-

include('../../inc/functions.php')

于 2013-09-15T05:58:23.170 回答
0
/*
 index.php
 /inc
    functions.php
 /css
 /img
 /connect
    db.class.php
    setup.php
 /admin
    index.php
    /img
    /css
    /inc
       functions.php
    /connect
       db.class.php
       setup.php 
    /content
       page.php
       /op
         add.php
         modify.php
         delete.php
  * 
 */
//try ABSOLUTE PATHS, see these examples... I considered your folder tree.
//for windows directories
//try this...

echo $_SERVER["DOCUMENT_ROOT"]. "\\index.php";
echo $_SERVER["DOCUMENT_ROOT"]. "\\inc\\functions.php";
echo $_SERVER["DOCUMENT_ROOT"]. "\\inc\\index.php";
echo $_SERVER["DOCUMENT_ROOT"]. "\\connect\\db.class.php";
echo $_SERVER["DOCUMENT_ROOT"]. "\\connect\\setup.php";
echo $_SERVER["DOCUMENT_ROOT"]. "\\admin\\index.php";
echo $_SERVER["DOCUMENT_ROOT"]. "\\admin\\inc\\functions.php";
echo $_SERVER["DOCUMENT_ROOT"]. "\\admin\\connect\\db.class.php";
echo $_SERVER["DOCUMENT_ROOT"]. "\\admin\\connect\\setup.php";
echo $_SERVER["DOCUMENT_ROOT"]. "\\admin\\content\\page.php";
echo $_SERVER["DOCUMENT_ROOT"]. "\\admin\\content\\op\\add.php";
echo $_SERVER["DOCUMENT_ROOT"]. "\\admin\\content\\op\\modify.php";
echo $_SERVER["DOCUMENT_ROOT"]. "\\admin\\content\\op\\delete.php";

//如果路径正确,总是使用它来包含文件。
//查看文件夹结构是否正确,有时先有项目的文件夹

include_once $_SERVER["DOCUMENT_ROOT"]. "\\index.php";
include_once $_SERVER["DOCUMENT_ROOT"]. "\\inc\\functions.php";
include_once $_SERVER["DOCUMENT_ROOT"]. "\\inc\\index.php";
include_once $_SERVER["DOCUMENT_ROOT"]. "\\connect\\db.class.php";
include_once $_SERVER["DOCUMENT_ROOT"]. "\\connect\\setup.php";
include_once $_SERVER["DOCUMENT_ROOT"]. "\\admin\\index.php";
include_once $_SERVER["DOCUMENT_ROOT"]. "\\admin\\inc\\functions.php";
include_once $_SERVER["DOCUMENT_ROOT"]. "\\admin\\connect\\db.class.php";
include_once $_SERVER["DOCUMENT_ROOT"]. "\\admin\\connect\\setup.php";
include_once $_SERVER["DOCUMENT_ROOT"]. "\\admin\\content\\page.php";
include_once $_SERVER["DOCUMENT_ROOT"]. "\\admin\\content\\op\\add.php";
include_once $_SERVER["DOCUMENT_ROOT"]. "\\admin\\content\\op\\modify.php";
include_once $_SERVER["DOCUMENT_ROOT"]. "\\admin\\content\\op\\delete.php";
于 2013-11-26T22:47:32.837 回答