简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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