如何知道在 PHP require_once() 语句中使用的正确路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/266688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:08:29  来源:igfitidea点击:

How do you know the correct path to use in a PHP require_once() statement

php

提问by dl__

As many do I have a config.php file in the root of a web app that I want to include in almost every other php file. So most of them have a line like:

我想在几乎所有其他 php 文件中包含一个 web 应用程序的根目录中的 config.php 文件。所以他们中的大多数人都有这样的一行:

require_once("config.php");

or sometimes

或有时

require_once("../config.php");

or even

甚至

require_once("../../config.php");

But I never get it right the first time. I can't figure out what php is going to consider to be the current working directory when reading one of these files. It is apparently not the directory where the file containing the require_once() call is made because I can have two files in the same directory that have different paths for the config.php.

但我从第一次就做对了。在读取这些文件之一时,我无法弄清楚 php 会认为当前工作目录是什么。它显然不是包含 require_once() 调用的文件所在的目录,因为我可以在同一目录中有两个文件,它们对 config.php 具有不同的路径。

How I have a situation where one path is correct for refreshing the page but an ajax can that updates part of the page requires a different path to the config.php in the require_once() statement;

我如何遇到这样一种情况,即一条路径对于刷新页面是正确的,但是更新部分页面的 ajax 罐需要在 require_once() 语句中使用不同的路径到 config.php;

What's the secret? From where is that path evaluated?

有什么秘密?从哪里评估该路径?

Shoot, I was afraid this wouldn't be a common problem - This is occurring under apache 2.2.8 and PHP 5.2.6 running on windows.

射击,我担心这不会是一个常见问题 - 这是在 Windows 上运行的 apache 2.2.8 和 PHP 5.2.6 下发生的。

回答by Seamus

The current working directory for PHP is the directory in which the called script file is located. If your files looked like this:

PHP 的当前工作目录是被调用的脚本文件所在的目录。如果您的文件如下所示:

/A
   foo.php
   tar.php
   B/
       bar.php

If you call foo.php (ex: http://example.com/foo.php), the working directory will be /A/. If you call bar.php (ex: http://example.com/B/bar.php), the working directory will be /A/B/.

如果您调用 foo.php(例如:http: //example.com/foo.php),则工作目录将为 /A/。如果您调用 bar.php(例如:http: //example.com/B/bar.php),工作目录将是 /A/B/。

There is where it gets tricky. Let us say that foo.php is such:

有它变得棘手的地方。让我们说 foo.php 是这样的:

<?php
require_once( 'B/bar.php' );
?>

And bar.php is:

bar.php 是:

<?php
require_once( 'tar.php');
?>

If we call foo.php, then bar.php will successfully call tar.php because tar.php and foo.php are in the same directory which happens to be the working directory. If you instead call bar.php, it will fail.

如果我们调用 foo.php,那么 bar.php 将成功调用 tar.php,因为 tar.php 和 foo.php 位于同一目录中,恰好是工作目录。如果您改为调用 bar.php,它将失败。

Generally you will see either in all files:

通常,您会在所有文件中看到:

require_once( realpath( dirname( __FILE__ ) ).'/../../path/to/file.php' );

or with the config file:

或使用配置文件:

// config file
define( "APP_ROOT", realpath( dirname( __FILE__ ) ).'/' );

with the rest of the files using:

其余文件使用:

require_once( APP_ROOT.'../../path/to/file.php' );

回答by bobwienholt

I like to do this:

我喜欢这样做:

require_once(dirname(__FILE__)."/../_include/header.inc");

That way your paths can always be relative to the current file location.

这样,您的路径始终可以相对于当前文件位置。

回答by Kent Fredric

I use the dirname(__FILE__)thing like bobwienholtmost the time, but what it could pay to do is have a base entry point that loads all your other code that defines a constant refereing to the root of the project, ie

我大部分时间都使用 像bobwienholt这样的 东西,但它可以做的是有一个基本入口点来加载所有其他代码,这些代码定义了对项目根目录的常量引用,即dirname(__FILE__)

define("ROOT",dirname(__FILE__).'/' ); 

and then later all you need to know is where the path is relative to root, ie:

然后你需要知道的是路径相对于根的位置,即:

require(ROOT . "/lib/tool/error.php"); 

note,

笔记,

你应该 REALLY真正避免以“../”开头的路径,它们不是相对于文件,而是相对于你的位置AREARE,这会产生坏掉的代码。

 cd foo
 php bar/baz.php 
 -> some error saying it cant find the file
 cd bar 
 php baz.php 
 -> suddenly working.

Important

重要的

If you use "../" notation, it takes completeignorance of the PHP Include Path, And ONLYconsiders where the person whom is running it is.

如果您使用“../”符号,则完全不知道PHP 包含路径,并且考虑运行它的人在哪里。

回答by Steve Massing

I include this code at the top of every page:

我在每个页面的顶部都包含此代码:

//get basic page variables
$self=$_SERVER['PHP_SELF']; 
$thispath=dirname($_SERVER['PHP_SELF']);
$sitebasepath=$_SERVER['DOCUMENT_ROOT'];

//include the global settings, variables and includes

include_once("$sitebasepath/globals/global.include.php");

Include and require both take either a relative path or the full rooted path. I prefer working with the full path and make all my references like the inlcude statement above. This allows me to enter a general variable $sitebasepath that handles account specific information that may change from machine to machine and then simply type the path from the webroot, ie. /globals/whatever_file.php

Include 和 require 都采用相对路径或完整的根路径。我更喜欢使用完整路径,并像上面的 inlcude 语句一样制作我的所有引用。这允许我输入一个通用变量 $sitebasepath 来处理可能因机器而异的帐户特定信息,然后只需键入来自 webroot 的路径,即。/globals/whatever_file.php

I also use the $self variable in forms that may call themselves to handle data input.

我还在可能调用自己处理数据输入的表单中使用 $self 变量。

Hope that helps.

希望有帮助。

回答by Ferdinand Beyer

If you have sufficient access rights, try to modify PHP's include_pathsetting for the whole site. If you cannot do that, you'll either have to route every request through the same PHP script (eg. using Apache mod_rewrite) or you'll have to use an "initialization" script that sets up the include_path:

如果您有足够的访问权限,请尝试修改include_path整个站点的PHP设置。如果您不能这样做,您要么必须通过相同的 PHP 脚本(例如使用 Apache mod_rewrite)路由每个请求,要么必须使用设置 include_path 的“初始化”脚本:

$includeDir = realpath(dirname(__FILE__) . '/include'); 
ini_set('include_path', $includeDir . PATH_SEPARATOR . ini_get('include_path'));

After that file is included, use paths relative to the include directory:

包含该文件后,使用相对于包含目录的路径:

require_once '../init.php'; // The init-script
require_once 'MyFile.php'; // Includes /include/MyFile.php

回答by Zak

Since require and require_once are very similar to include and include_once, all the documentation is posted under the "include" functions doc area on php.net From that page

由于 require 和 require_once 与 include 和 include_once 非常相似,因此所有文档都发布在 php.net 上的“include”函数文档区域下从该页面

Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script. E.g. if your include_path is libraries, current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/libraries/ and then in /www/include/. If filename begins with ./ or ../, it is looked only in the current working directory.

首先在相对于当前工作目录的每个 include_path 条目中查找要包含的文件,然后在当前脚本的目录中查找。例如,如果您的 include_path 是库,当前工作目录是 /www/,则您包含了 include/a.php 并且该文件中包含“b.php”,则首先在 /www/libraries/ 中查看 b.php,然后在/www/包括/。如果文件名以 ./ 或 ../ 开头,则仅在当前工作目录中查找。

Further, you can find all the current include paths by doing a "php -i" from the command line. You can edit the include path in your php.ini file, and also via ini_set(). You can also run the php_info() function in your page to get a printout of your env vars if the CLI is inconvenient.

此外,您可以通过从命令行执行“php -i”来找到所有当前包含路径。您可以在 php.ini 文件中编辑包含路径,也可以通过 ini_set()。如果 CLI 不方便,您还可以在页面中运行 php_info() 函数以获取环境变量的打印输出。

回答by Thomas Owens

The only place I've seen the path evaluated from is the file that you are currently editing. I've never had any problems with it, but if you are, you might want to provide more information (PHP version, OS, etc).

我看到评估路径的唯一地方是您当前正在编辑的文件。我从来没有遇到过任何问题,但如果是,您可能需要提供更多信息(PHP 版本、操作系统等)。

回答by Michael Luton

The path of the PHP file requested in the original GET or POST is essentially the 'working directory' of that script. Any "included" or "required" scripts will inherit that as their working directory as well.

原始 GET 或 POST 中请求的 PHP 文件的路径本质上是该脚本的“工作目录”。任何“包含”或“必需”脚本也将继承它作为它们的工作目录。

I will either use absolute paths in require statements or modify PHP's include_path to include any path in my app I may want to use to save me the extra typing. You'll find that in php.ini.

我将在 require 语句中使用绝对路径,或者修改 PHP 的 include_path 以在我的应用程序中包含任何我可能想要用来节省额外输入的路径。您会在 php.ini 中找到它。

include_path = ".:/list/of/paths/:/another/path/:/and/another/one"

include_path = ".:/list/of/paths/:/another/path/:/and/another/one"

I don't know if it'll help you out in this particular instance but the magical constants like FILEand DIRcan come in handy if you ever need to know the path a particular file is running in.

我不知道在这个特定的情况下它是否会帮助你,但是如果你需要知道特定文件运行的路径,像FILEDIR这样的神奇常量会派上用场。

http://us2.php.net/manual/en/language.constants.predefined.php

http://us2.php.net/manual/en/language.constants.predefined.php

回答by andyhky

Take a look at the function getcwd. http://us2.php.net/getcwd

看看函数getcwd。http://us2.php.net/getcwd