PHP 包含绝对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22912121/
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-25 16:21:33  来源:igfitidea点击:

PHP include absolute path

phpincluderelative-pathinclude-pathabsolute-path

提问by digicom

I have a variable on my site called $basePath which is set as:

我的网站上有一个名为 $basePath 的变量,它设置为:

$basePath = '/Systems/dgw/';

I am using it on all my css, js and images tags as so (shortened for better visibility):

我在我所有的 css、js 和图像标签上都使用它(缩短以获得更好的可见性):

<link href="<?php echo $basePath; ?>include/assets/css/bootstrap.min.css">

I have no problem with those includes and they work fine in wherever file and in whatever folder I am.

我对这些包含没有问题,它们在我所在的任何文件和文件夹中都可以正常工作。

I have a certain included page which has the following line:

我有一个包含以下行的页面:

<img src="<?php echo $basePath; ?>images/new_logo.png" alt="logo"/>

And the image shows just fine. The line after it states:

图像显示得很好。它后面的行指出:

<?php include($basePath.'include/assets/common/topMessages.php');?>

But the include doesn't happens. When I try it like this:

但是包含不会发生。当我这样尝试时:

<?php include('../../include/assets/common/topMessages.php');?>

It works.

有用。

Anybody has any idea what could be wrong?

任何人都知道什么可能是错的?

回答by Laurent S.

You can't include php files relatively to your webroot that way, cause if you use the slash as first character, the reference will go much deeper than just your document root. So, instead of using your basepath, you could do something like this :

您不能以这种方式包含相对于您的 webroot 的 php 文件,因为如果您使用斜杠作为第一个字符,则引用将比您的文档根目录更深入。因此,您可以执行以下操作,而不是使用您的基本路径:

<?php 
   $path = $_SERVER['DOCUMENT_ROOT'];
   $path .= "/yourpath/yourfile.php";
   include_once($path);
?>

回答by digicom

If your server doesn't populate the "document_root", you may need this

如果您的服务器没有填充“document_root”,您可能需要这个





    require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/path/to/file.php");

I use this line of code. It goes back to the "top" of the site tree, then goes to the file desired.
For example, let's say i have this file tree:
domain.com/aaa/index.php
domain.com/bbb/ccc/ddd/index.php
domain.com/_resources/functions.php

I can include the functions.php file from wherever i am, just by copy pasting

我使用这行代码。它返回到站点树的“顶部”,然后转到所需的文件。
例如,假设我有这个文件树:
domain.com/aaa/index.php
domain.com/bbb/ccc/ddd/index.php
domain.com/_resources/functions.php

我可以包含functions.php文件无论我在哪里,只需复制粘贴

require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/_resources/functions.php");



If you need to use this code many times, you may create a function that returns the "str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))" part. Then just insert this function in the first file you include. I have an "initialize.php" file that i include at the very top of each php page and which contains this function. The next time i have to include files, i in fact just use the function (named "path_back"):



如果您需要多次使用此代码,您可以创建一个返回“str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))”部分的函数。然后只需将此函数插入​​您包含的第一个文件中。我有一个“initialize.php”文件,它包含在每个 php 页面的最顶部,其中包含此函数。下次我必须包含文件时,我实际上只是使用函数(名为“path_back”):

require(path_back()."/_resources/another_php_file.php");

回答by raphvon

You can add an include_path = ".:/home/myuser/mysite.com/"to your php.inior you can add something like this into your script before the includeor require:

您可以在您的脚本中添加一个include_path = ".:/home/myuser/mysite.com/"php.ini或者您可以在您的脚本中在includeor之前添加类似的内容require

set_include_path(get_include_path() . ":/home/myuser/mysite.com/");

The first one will work for all the scripts running in your website.

第一个适用于您网站中运行的所有脚本。

The second option will only work for the script which has the setincludepathon the code, for the rest of the application it will not work unless you have an object you call in every script that add the setincludepath.

第二个选项仅适用于setincludepath代码中包含setincludepath.