【问题标题】:Path Include error (failed to open stream - PHP)路径包含错误(无法打开流 - PHP)
【发布时间】:2018-09-08 05:57:18
【问题描述】:

我正在从头开始在后台工作,这里有一些文件夹示例

管理 > 包含 > global_variable.php

管理 > 滑块 > variable.php

管理 > 滑块 > 上传功能 > index.php

在 index.php 我有

<?php include('../variable.php'); ?>

在 variable.php 我有

<?php include('../include/global_variable.php');?>

但是 index.php 没有读取 global_variable.php

它给了我以下错误

"Warning: include(../include/global_variable.php): failed to open stream: No such file or directory in [irrelevant]\Admin\sliders\variable.php on line <i>1</i>"

不,这不是正斜杠和反斜杠的问题,因为它在不同的架构上工作(我正在链接 ../variable.php 和 ../../include/global_variable.php在我的 index.php 上)

从逻辑上讲,这应该有效,但事实并非如此。 我的假设是它正在从 index.php 的目录中读取“../include/global_variable.php”(它应该从 variable.php 中读取)

在保持相同文件结构的同时有什么解决方法吗?

谢谢

【问题讨论】:

标签: php


【解决方案1】:

您遇到了相对路径问题。

当包含 PHP 文件时,使用的路径是相对于正在执行的脚本(不是包含的)。

因此,在您的情况下,您将包含来自 Admin/sliders/uploadfunction 的文件,因此这是您的“基本路径”。那么:

  • 在滑块目录中查找../variable.php。
  • 在滑块目录中查找../include/global_variable.php。

如 cmets 中所说,尝试在 variable.php 中使用常量 __DIR__ 包括:

&lt;?php include(__DIR__.'/../include/global_variable.php');?&gt;

更多关于 PHP 常量的信息:http://php.net/manual/en/language.constants.predefined.php

更多关于相对路径问题:http://yagudaev.com/posts/resolving-php-relative-path-problem/

【讨论】:

  • 这解决了我的问题!感谢您的反馈。
猜你喜欢
  • 2015-05-10
  • 1970-01-01
  • 2013-06-12
  • 1970-01-01
  • 2017-05-09
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多