PHP相对路径:我可以配置它吗? [英] PHP relative path: can I configure it?

查看:103
本文介绍了PHP相对路径:我可以配置它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tl; dr:如何从当前文件的角度使PHP解释include/require语句中的相对路径?

tl;dr: How do I make PHP interpret relative paths in include/require statement from the perspective of the current file?

这是关于PHP中有关相对路径的旧问题的又一个问题.请忍受,因为我找不到我要尝试做的任何解决方案.

This is yet another question about that old issue in PHP about relative paths. Please bear with me, as I couldn't find any solution for what I am specifically trying to do.

请考虑以下目录树和文件:

Consider the following directory tree and files:

[www]:
    index.php
    config.php
    [webroot]:
        home.php

index.php需要home.php,可在webroot中找到:

require('webroot/home.php');

home.php要求config.php,可在父目录中找到:

home.php requires config.php, found in the parent directory:

require('../config.php');

我的问题是,这在我的本地开发环境(Ubuntu 14.04 LTS/15.10)中不起作用,而在生产中可以完美地运行.每个提到的环境都在运行Apache 2和PHP5.
奇怪的是,当我在Vagrant VM(Ubuntu 12.04 LTS)中运行它并从主机访问它时,它确实在本地运行.但是,现在,我无法在此处运行VM.

My problem is that this won't work in my local development environment (Ubuntu 14.04 LTS / 15.10), whereas it runs flawlessly in production. Every mentioned environment is running Apache 2 and PHP 5.
Strangely, this does run locally when I run it inside my Vagrant VM (Ubuntu 12.04 LTS), accessing it from the host machine. But, right now, I cannot run a VM here.

那么,为什么这些环境的行为如此不同?
这使我相信必须有一种方法来更改PHP解释相对路径的方式.我目前正在使用一个6GB +的PHP项目,其编写方式与上面的示例类似,我真的需要避免花费我大量的精力来重写每一个include/require语句(使用dirname(__FILE__)左右),以及git merge可能导致此可能冲突.

So, why do these environments behave so differently?
This makes me believe that there must be a way to change how PHP interprets relative paths. I am currently working with a 6GB+ PHP project that is written like the example above, and I really need to avoid the amount of effort that it'll take from me to rewrite every include/require statement (using dirname(__FILE__) or so), as well as the git merge conflicts this might cause.

编辑:我刚刚记得我实际上已经在这里问过这个问题: PHP:在内部包含包含的文件

EDIT: I've just remembered I actually had already asked this question here: PHP: include inside included file

推荐答案

用于解析此类相对URL的路径由 set_include_path .

The path used to resolve relative URLs like this is configured by the include_path configuration option which has a dedicated function for setting it at runtime: set_include_path.

请注意,搜索路径集可能包含.,代表当前工作目录",可以使用 getcwd .您可能还需要更改此设置,以使像./foo.php../foo.php这样的相对路径可以正常工作.

Note that the set of paths to search may include ., representing the "current working directory", which can be set with chdir and read with getcwd. You may also need to change this to make explicitly relative paths like ./foo.php and ../foo.php to work.

(我本来建议您改用__DIR__$_SERVER['DOCUMENT_ROOT'],但是您提到您不想重写现有代码.我仍然建议其他人阅读此代码,以便在每个include位置都明确指出路径是相对的,以避免动态基础产生奇怪的错误和潜在的安全漏洞.)

(I was going to recommend you used __DIR__ or $_SERVER['DOCUMENT_ROOT'] instead, but you mention that you don't want to rewrite existing code. I would still recommend to anyone else reading this to make explicit in each include where paths are relative to, to avoid odd bugs and potential security holes with the dynamic base.)

这篇关于PHP相对路径:我可以配置它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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