如何在文件根目录外包含文件? [英] How to include file outside document root?

查看:130
本文介绍了如何在文件根目录外包含文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是将'domain1'中的'file1.php'包含在'domain2'上的'file2.php'中。所以我认为我应该这样做:

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:

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 ?

谢谢

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

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

此外,我已尝试使用safe_mode On和Off。

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

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

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.

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

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:

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

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

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:

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

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

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

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

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

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

谢谢大家!

推荐答案

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

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

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

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.

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

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

这篇关于如何在文件根目录外包含文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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