简体   繁体   English

PHP:包含在包含文件中

[英]PHP: include inside included file

I've gotta solve this once and for all.我必须一劳永逸地解决这个问题。

I'm currently working on a 4 years old project, written in PHP and that is run on Apache server.我目前正在从事一个 4 年前的项目,该项目是用 PHP 编写的,并在 Apache 服务器上运行。
We are a team of about 40 folks and each of us uses the prefered OS, so we have Windows, Macs and Linuxes running this code.我们是一个大约 40 人的团队,我们每个人都使用首选的操作系统,所以我们有 Windows、Mac 和 Linux 运行此代码。 I'm among those who prefer Ubuntu.我是喜欢 Ubuntu 的人之一。

Anyway, everytime I clone this project, I have to modify some portions of code so it'll work in my machine, because of the following old include / require issue:无论如何,每次我克隆这个项目时,我都必须修改代码的某些部分,以便它可以在我的机器上工作,因为以下旧的include / require问题:
So suppose I have the following files and dirs:所以假设我有以下文件和目录:

root/
|   index.php
|   framework/
    |   index.php
    |   framework.php
|   config.php

Summarizing:总结:

  • root/index.php does require('framework/index.php') root/index.php确实require('framework/index.php')
  • root/framework/index.php does require('../config.php') and require('framework.php') root/framework/index.php确实require('../config.php')require('framework.php')
  • Obviously, the code above gives me an error on the first require , because it's looking for config.php in the directory in which root folder is located at.显然,上面的代码在第一个require上给了我一个错误,因为它在root文件夹所在的目录中寻找config.php

Yes, I do know that we should use some constants like define('ROOT_DIR', path) or use dirname(__DIR__) inside requires.是的,我确实知道我们应该define('ROOT_DIR', path)使用一些常量,例如define('ROOT_DIR', path)或 use dirname(__DIR__) My problem is that this very code runs flawlessly on some machines around here (including the production server, which also runs Ubuntu LTS), so it's believed that this error is due to an Apache or PHP configuration .我的问题是,这段代码可以在附近的一些机器上完美运行(包括生产服务器,它也运行 Ubuntu LTS),所以相信这个错误是由于 Apache 或 PHP 配置造成的 I must run this code locally for development, but there're just too many include and require , and they won't let me change all of the original project's require so it contains dirname(__DIR__) or whatever bu****it in it, as it's believed to be unecessary.我必须在本地运行此代码进行开发,但是includerequire太多了,他们不会让我更改所有原始项目的require因此它包含dirname(__DIR__)或任何 bu****it它,因为它被认为是不必要的。

So, please don't suggest me to do anything to the preexisting require calls.所以,请不要建议我对预先存在的require调用做任何事情。 I need to know how to change PHP (or maybe another service's) configuration so when it requires files, it'll check them in relation to the script that is requiring the files, not in relation to the very first script that started this require chain.我需要知道如何更改 PHP(或者可能是其他服务的)配置,因此当它需要文件时,它会根据需要文件的脚本来检查它们,而不是与启动此require链的第一个脚本有关.

Maybe this is not relevant, but everytime I reinstall Apache2 and PHP5 on my Ubuntu LTS 14.04, its default configurations are so that this code doesn't run.也许这无关紧要,但是每次我在 Ubuntu LTS 14.04 上重新安装 Apache2 和 PHP5 时,其默认配置都不会运行此代码。 One of my coworkers, who uses Windows (used to run 8.1, now he's running 10), never has this problem, as in Windows apparently the XAMP or WAMP or whatever he uses has default configurations that will allow this code to work.我的一位同事使用 Windows(曾经运行 8.1,现在他运行 10),从来没有这个问题,因为在 Windows 中,XAMP 或 WAMP 或他使用的任何东西显然都有允许此代码工作的默认配置。

Please help.请帮忙。 Thanks in advance.提前致谢。

Maybe someone tempered with the include_path on the production machine.也许有人对生产机器上的 include_path 进行了调整。 You could do the same on your development machine:你可以在你的开发机器上做同样的事情:

set-include-path设置包含路径

or set the include_path in your php.ini: include_path或者在你的 php.ini 中设置 include_path: include_path

I have found another interesting thing on require-statements that have nested require-statements: comment in php-documentation, second from the top :我在嵌套了 require-statements 的 require-statements 上发现了另一个有趣的事情: php-documentation 中的注释,从顶部第二个

When using symbolic links with PHP, specify a dotslash './page.php' path to ensure that PHP is looking in the right directory with nested requires:在 PHP 中使用符号链接时,指定一个点斜线“./page.php”路径以确保 PHP 在正确的目录中查找嵌套的要求:

Eg when the required actual page1.php contains other require statements to, say page2.php, PHP will search the path that the symbolic link points to, instead of the path where the symbolic link lives.例如,当所需的实际 page1.php 包含其他 require 语句时,例如 page2.php,PHP 将搜索符号链接指向的路径,而不是符号链接所在的路径。 To let PHP find the other page2.php in the path of the symbolic link, a require('./page2.php');为了让PHP在符号链接的路径中找到另一个page2.php,一个require('./page2.php'); statement will solve the puzzle.声明将解决这个难题。

Maybe one of these might help.也许其中之一可能会有所帮助。

The only reason that the first require would fail is that either your include path does not have '.'第一个要求失败的唯一原因是您的包含路径没有“。” Or the file itself is not readable (eg due to permissions).或者文件本身不可读(例如由于权限)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM