警告:include(../../ inc / functions.php):无法打开流:没有这样的文件或目录

时间:2013-09-15 05:04:03

标签: php include

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

 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文件包含到文件夹内容或op中。我已经用不同的方式进行了测试,并从这里检查了一些答案我无法修复它。

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

3 个答案:

答案 0 :(得分: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 pathinclude所需的所有其他文件只需使用该功能并传入absolute path的{​​{1}}

注意:

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

file

[root] [admin] [index.php] [url.php] [css] [site.css] 内,您需要包含index.php

这样的内容
site.css

答案 1 :(得分:0)

我认为你在路径上遗漏了一个../ ,即

您必须使用以下代码 -

include('../../../inc/functions.php')如果您想将文件 function.php 添加到 /op 文件夹中的任何文件中,并将其包括在内 /content 文件夹中的文件,您必须使用以下文件 -

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

答案 2 :(得分: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";