PHP:包含在包含文件中 [英] PHP: include inside included file

查看:44
本文介绍了PHP:包含在包含文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须一劳永逸地解决这个问题.

我目前正在从事一个已有 4 年历史的项目,该项目用 PHP 编写并在 Apache 服务器上运行.
我们是一个大约 40 人的团队,我们每个人都使用首选的操作系统,所以我们有 Windows、Mac 和 Linux 运行此代码.我是喜欢 Ubuntu 的人之一.

无论如何,每次我克隆这个项目时,我都必须修改一些代码部分才能在我的机器上运行,因为下面是旧的include/require问题:
所以假设我有以下文件和目录:

root/|索引.php|框架/|索引.php|框架.php|配置文件

总结:

  • root/index.phprequire('framework/index.php')
  • root/framework/index.phprequire('../config.php')require('framework.php')代码>
  • 显然,上面的代码在第一个 require 上给了我一个错误,因为它在 root 所在的目录中寻找 config.php> 文件夹位于.

是的,我知道我们应该使用一些常量,例如 define('ROOT_DIR', path) 或在 requires 中使用 dirname(__DIR__) .我的问题是这段代码可以在附近的一些机器上完美运行(包括生产服务器,它也运行 Ubuntu LTS),所以相信这个错误是由于 Apache 或 PHP配置.我必须在本地运行此代码进行开发,但是includerequire 太多了,他们不会让我更改所有原始项目的require 所以它包含 dirname(__DIR__) 或其中包含的任何内容,因为它被认为是不必要的.

所以,请不要建议我对预先存在的 require 调用做任何事情.我需要知道如何更改 PHP(或者可能是其他服务的)配置,因此当它需要文件时,它会根据需要文件的脚本来检查它们,而不是与启动此 需要链.

也许这无关紧要,但每次我在 Ubuntu LTS 14.04 上重新安装 Apache2 和 PHP5 时,其默认配置都不会运行此代码.我的一位同事使用 Windows(以前运行 8.1,现在他运行 10),从来没有这个问题,因为在 Windows 中,XAMP 或 WAMP 或他使用的任何东西显然都有允许此代码工作的默认配置.

请帮忙.提前致谢.

解决方案

也许有人对生产机器上的 include_path 进行了调整.你可以在你的开发机器上做同样的事情:

设置包含路径

或者在你的 php.ini 中设置 include_path:include_path

我在嵌套 require-statements 的 require-statements 上发现了另一个有趣的事情:php 文档中的评论,从上数第二个:

<块引用>

在 PHP 中使用符号链接时,指定一个点斜线./page.php"路径,以确保 PHP 在正确的目录中查找嵌套要求:

例如当所需的实际 page1.php 包含其他 require 语句时,例如 page2.php,PHP 将搜索符号链接指向的路径,而不是符号链接所在的路径.为了让PHP在符号链接的路径中找到另一个page2.php,一个require('./page2.php');声明将解决难题.

也许其中之一可能会有所帮助.

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.
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. I'm among those who prefer 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:
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/framework/index.php does require('../config.php') and 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.

Yes, I do know that we should use some constants like define('ROOT_DIR', path) or use dirname(__DIR__) inside requires. 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. 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.

So, please don't suggest me to do anything to the preexisting require calls. 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.

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. 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.

Please help. Thanks in advance.

解决方案

Maybe someone tempered with the include_path on the production machine. You could do the same on your development machine:

set-include-path

or set the include_path in your php.ini: include_path

I have found another interesting thing on require-statements that have nested require-statements: comment in php-documentation, second from the top:

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:

E.g. 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. To let PHP find the other page2.php in the path of the symbolic link, a require('./page2.php'); statement will solve the puzzle.

Maybe one of these might help.

这篇关于PHP:包含在包含文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆