【问题标题】:PHP: A working file doesn't find other files after inclusion?PHP:一个工作文件在包含后找不到其他文件?
【发布时间】:2013-04-13 11:52:53
【问题描述】:

首先:抱歉标题。如果你可以弥补它,请这样做,因为我真的不知道如何命名我的问题。也就是说,周围很可能有重复,但我想我需要克服这个问题,只是不知道要搜索什么......

基地不是我做的。它主要是用 HTML 编写的,但包含用 PHP 完成的,我真的不喜欢它的制作方式。但这与我在这里要问的问题无关。

这里描述的文件包含的内容比这里写的要多,但这里的问题是
当需要文件 A 需要文件 B 需要许多文件时,文件 B 找不到它正在查找的文件为。

假设我有一个像这样的目录树:

| - Project
+--- lower.txt
| -- Stuff
| ---- Case
+------ index.php
+------ upper.php
| -- Gallery
+---- main.php
+---- config.php
+---- miscfunc.php
+---- css.php

项目\Stuff\Case\index.php:

<?php include("./upper.php");?> ...
<?php include("../../lower.txt");?>

项目\Stuff\Case\upper.php:

<?php require_once("../../Gallery/main.php"); ?>

项目\画廊\main.php:

<?php namespace Gallery;
...
// I'm just trying to debug something here... :
// -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- //

$PathToScan = "./"; // Scan current directory, will point to /Project/Stuff/Case/

function ScanFolder($path) {
    $Array = array();
    if ($dir = @opendir($path)) {
        while (($file = readdir($dir)) !== FALSE) {
            $Array[] = $file;
        }
        printf("Current directory: %s<br/>\n", dirname(__FILE__));
        printf("document_root: %s<br/>\n", $_SERVER["DOCUMENT_ROOT"]);
        printf("Scan directory: %s<br/>\n<br/>\n", $path);
        var_dump($Array);
    }
}
ScanFolder($PathToScan);

// -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- //

require_once("./config.php"); // These files are also in Gallery namespace
require_once("./miscfunc.php");
require_once("./css.php"); ...

...
?>

所以...当我执行 Project\Stuff\Case\index.php 我得到:

Current directory: C:\xampp\htdocs\Project\Gallery
document_root: C:/xampp/htdocs
Scan directory: ./

array(4) { [0]=> string(1) "." [1]=> string(2) ".." [2]=> string(9) "index.php" [3]=> string(7) "upper.php" }
Warning: require_once(./config.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\Project\Gallery\main.php on line 44

Fatal error: require_once() [function.require]: Failed opening required './config.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\Project\Gallery\main.php on line 44

现在为什么会发生这种情况,我怎样才能在不移动任何文件的情况下让它工作,最好不破坏 main.php?

【问题讨论】:

  • 大字体被否决,即使在编辑被批准后也是如此。如果我们能读懂您的字体,我们就可以很好地阅读您的问题,而不会显得粗体和麻烦。
  • 对...也许你只是有一个小屏幕和 f***ed 字体设置,或者对大于 9 pt 的字体进行了严重的添加。它没有那么大。
  • 对不起,这太荒谬了......

标签: php include relative-path require


【解决方案1】:

我改变了

require_once("./filename.php");

行到

require_once(__DIR__ . "/filename.php");


仍然想知道是否可以这样做......

【讨论】:

    猜你喜欢
    • 2016-03-20
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 2021-10-19
    相关资源
    最近更新 更多