PHP的:包括里面包含的文件 [英] PHP: include inside included file

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

问题描述

我得解决这个问题一劳永逸。

I've gotta solve this once and for all.

我目前工作的一个4岁的项目,用PHP编写的,并且是Apache服务器上运行。结果
我们是一个团队约40人与我们每个人的使用prefered OS,因此,我们有Windows,Mac和Linux版本上运行该code。我是那些谁preFER的Ubuntu之间。

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.

反正每次我克隆这个项目,我必须修改code的某些部分所以它会在我的机器上工作,因为,以下旧包含 / 要求问题:结果
因此,假设我有以下的文件和显示目录:

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

汇总:


  • 根/ index.php文件确实规定(框架/的index.php')

  • 根/框架/ index.php文件确实要求('../ config.php文件')要求('framework.php')

  • 显然,code上面给我的第一个错误要求,因为它是寻找的config.php 在其文件夹位于该目录。

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

是的,我知道,我们应该使用一些常量,比如定义('ROOT_DIR,路径)或使用目录名(__ DIR __)里面需要。我的问题是,这个非常code完美地运行在一些机器在这里(包括生产服务器,这也运行Ubuntu的LTS),所以它认为的这个错误是由于一个Apache或者PHP配置即可。
我必须为发展本地运行此code,但只是太多的包含要求,和他们不会让我改变所有原始项目的要求使其包含目录名的(__ DIR __)或任何BU * ***它它,因为它被认为是不必要的。

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.

所以,请不要建议我做任何事情到preexisting 要求来电。我需要知道如何更改PHP(或者其他服务的)的配置,所以当它需要的文件,它会就该被要求文件的脚本检查它们,而不是就该开始了这个非常第一个脚本要求链。

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.

也许这是不相关的,但每次我重新安装的Apache2和PHP5在我的Ubuntu 14.04 LTS,其默认的配置,都让这个code不运行。我的一个同事,谁使用的Windows(用于运行8.1,现在他跑10),从来没有这个问题,因为在Windows显然XAMP或WAMP或任何他使用了缺省配置,使这个code工作

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.

请帮忙。先谢谢了。

推荐答案

也许有人在生产机器上的include_path中锻炼。你可以做同样的在开发机器上:

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

设置包括路径

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

or set the include_path in your php.ini: include_path

我发现在该嵌套要求,语句的另一个有趣的事情需要语句:
PHP的文档中评论,第二次从顶部

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

在使用符号链接,使用PHP,指定dotslash./p​​age.php路径,以确保PHP正在与嵌套正确的目录要求:

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:

例如。当需要实际包含page1.php中要求的其他报表,则page2.php说,PHP将搜索,而不是路径中的符号链接生活所在的路径,该符号链接指向。为了让PHP找到符号链接的路径则page2.php等,一个要求('./使page2.php');声明将解决这一难题。

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天全站免登陆