php 如何在文档根目录外包含文件?

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

How to include file outside document root?

phpincludeinclude-path

提问by Brayn

What I want do to is to include 'file1.php' from 'domain1' into 'file2.php' on 'domain2'. So what I figured I should do is something like this:

我想要做的是将“file1.php”从“domain1”包含到“domain2”上的“file2.php”中。所以我认为我应该做的是这样的:

file2.php
require_once '/var/www/vhosts/domain1/httpdocs/file1.php';

But this won't work for reasons I can't truly grasp. So what I did was to add my path to the include path. Something like:

但由于我无法真正理解的原因,这将不起作用。所以我所做的是将我的路径添加到包含路径中。就像是:

file2.php
set_include_path(get_include_path() . PATH_SEPARATOR . "/var/www/vhosts/domain1/httpdocs");
require_once 'file1.php';

So can you please give me some hints as of where I'm doing wrong ?

那么你能给我一些关于我做错的地方的提示吗?

Thanks

谢谢

UPDATE - Either way I get the following error message:

更新 - 无论哪种方式,我都会收到以下错误消息:

Fatal error: require() [function.require]: Failed opening required '/var/www/vhosts/domain1/httpdocs/file1.php' (include_path='.:/php/includes:/usr/share/pear/') in /var/www/vhosts/domain2/httpdocs/file2.php on line 4

Also I have tried this both with safe_mode On and Off.

我也尝试过打开和关闭安全模式。

UPDATE2: Also I've changed the permissions to 777 on my test file and I've double-checked the paths to the include file in bash.

UPDATE2:此外,我已将测试文件的权限更改为 777,并且我已在 bash 中仔细检查了包含文件的路径。

SOLUTION: I've managed to solve the mystery! My hosting company uses Plesk to manage domains and such. Also the error reporting level in php.ini was not E_ALL. When I set error reporting to E_ALL I got a warning saying:

解决方案:我设法解开了这个谜团!我的托管公司使用 Plesk 来管理域等。php.ini 中的错误报告级别也不是 E_ALL。当我将错误报告设置为 E_ALL 时,我收到一条警告:

Warning: require() [function.require]: open_basedir restriction in effect.

So I went in /var/www/vhosts/domain2/conf/httpd.include and edited the open_basedir path. Note that this is not a durable solution since this config file is rewritten by plesk each time the domain config is changed. What you should do is edit (or create) the 'vhost.conf' file in the same directory and then run:

所以我进入 /var/www/vhosts/domain2/conf/httpd.include 并编辑了 open_basedir 路径。请注意,这不是一个持久的解决方案,因为每次更改域配置时,plesk 都会重写此配置文件。您应该做的是在同一目录中编辑(或创建)“vhost.conf”文件,然后运行:

 /usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=DOMAIN.TLD

This should reconfigure the settings for your domain but for some strange reason it won't work with open_basedir. I can modify other things like document_root but it won't change open_basedir, but that's another problem :D

这应该为您的域重新配置设置,但由于某些奇怪的原因,它不适用于 open_basedir。我可以修改其他内容,例如 document_root,但不会更改 open_basedir,但这是另一个问题:D

SOLUTION FINAL: For those with the same problem here is the final code that worked. I just added this in /var/www/vhosts/domain2/conf/vhost.conf (you can change '/var/www/vhosts' to '/' or anything you like):

解决方案最终:对于那些有同样问题的人,这里是最终有效的代码。我刚刚在 /var/www/vhosts/domain2/conf/vhost.conf 中添加了这个(你可以将 '/var/www/vhosts' 更改为 '/' 或任何你喜欢的):

    <Directory /var/www/vhosts/DOMAIN.TLD/httpdocs>
    <IfModule mod_php5.c>
            php_admin_flag engine on
            php_admin_flag safe_mode off
            php_admin_value open_basedir "/var/www/vhosts"
    </IfModule>
            Options -Includes -ExecCGI
    </Directory>

Thank you all guys!

谢谢大家!

采纳答案by Tim Post

You can not accomplish this if open_basediris in effect, which prevents PHP from traversing out of the home directory.

如果open_basedir生效,则无法完成此操作,这会阻止 PHP 遍历主目录。

What you can do is make sure that docroot1 and docroot2 are owned by users in the same group, set group permissions accordingly and use a symbolic link from docroot2 to docroot1 to read the other web root.

您可以做的是确保 docroot1 和 docroot2 归同一组中的用户所有,相应地设置组权限并使用从 docroot2 到 docroot1 的符号链接来读取另一个 Web 根。

Or, re-build PHP and let it just follow typical *nix permissions like every other process :)

或者,重新构建 PHP,让它像其他所有进程一样遵循典型的 *nix 权限:)

回答by Kristoffer Sall-Storgaard

This works on a couple of machines i manage

这适用于我管理的几台机器

ini_set("include_path",".:/hsphere/local/home/user_name/other_domain.com");
require "filename.php";

回答by Pekka

You can include files from anywhere you want, unless your PHP script's permissions, or the safe modeprevent it. Your first approach is perfectly fine. What errors do you get?

您可以从任何地方包含文件,除非您的 PHP 脚本权限或安全模式阻止它。您的第一种方法非常好。你有什么错误?

Re the comments, that seem to confirm that there is no access from within PHP to a file that definitely exists. As to what it could be, Suhosin having been ruled out, the only thing I can think of is PHP or Apache being some kind of a a chroot Jail:

重新评论,这似乎证实了从 PHP 内部无法访问确实存在的文件。至于它可能是什么,Suhosin 已被排除在外,我唯一能想到的是 PHP 或 Apache 是某种 aa chroot Jail

The main benefit of a chroot jail is that the jail will limit the portion of the file system the daemon can see to the root directory of the jail. Additionally, since the jail only needs to support Apache, the programs available in the jail can be extremely limited. Most importantly, there is no need for setuid-root programs, which can be used to gain root access and break out of the jail.

chroot jail 的主要好处是,jail 会将守护进程可以看到的文件系统部分限制在 jail 的根目录中。此外,由于jail 只需要支持Apache,jail 中可用的程序可能非常有限。最重要的是,不需要 setuid-root 程序,它可以用来获得 root 访问权限和越狱。

I have never worked with anything like this so I can't tell you how to spot it (apart from doing a glob()on /var/www/vhostsand see what comes up. But I think this would have to have been set up by an administrator. Who runs your machine?

我从来没有与这样的事工作,所以我不能告诉你如何从做一个发现它(除了glob()/var/www/vhosts看看会发生什么吧。但我认为这必须已经由管理员进行设置。谁运行你的机器?

回答by naugtur

I sit here wondering why You didn't jus do a symlink. Or did I mis something? You could symlink a folder with needed includes to the path You have access to.

我坐在这里想知道你为什么不做一个符号链接。还是我错过了什么?您可以将包含所需包含的文件夹符号链接到您有权访问的路径。

回答by coder

what you are using is bad practice. put the dependent code into domain specific. Duplicating the same code is the right approach as it does not affect the operation of both sites.

你使用的是不好的做法。将依赖代码放入特定领域。复制相同的代码是正确的方法,因为它不会影响两个站点的运行。

Try creating the symlink of file1.php and included that as if it is from the local directory.

尝试创建 file1.php 的符号链接,并将其包含在本地目录中。

also make sure that .htaccess has the followsymlink option set to true

还要确保 .htaccess 的 followsymlink 选项设置为 true

how about trying this in your file2.php in domain2?

在 domain2 中的 file2.php 中尝试这个怎么样?

require_once '../../../domain1/httpdocs/file1.php';

require_once '../../../domain1/httpdocs/file1.php';

回答by douwe

Try chmod 777 on a test php file to see if that works, if it does you have permission issues. Also do a simple phpinfo() and see if save mode is on.

在测试 php 文件上尝试 chmod 777 以查看它是否有效,如果你有权限问题。也做一个简单的 phpinfo() 并查看保存模式是否打开。

回答by Chuck Burgess

What happens if you try to require a different file:

如果您尝试要求不同的文件,会发生什么情况:

// test.php
<?php
   echo 'Hello World';
?>

// your file
require_once('test.php');

Does that work? If so, put test.php in the other location and try it again. Does it still work?

那样有用吗?如果是这样,请将 test.php 放在另一个位置,然后再试一次。它仍然有效吗?