简体   繁体   English

PHP:包含来自其他根目录的文件

[英]PHP: Include file from different root directory

I have 2 root directories for a site, httpdocs and httpsdocs. 我有2个网站根目录,即httpdocs和httpsdocs。 I am sure its obvious what the 2 are for. 我确信2的用途很明显。 But I want to keep things consistent through-out the site like global navigation. 但我想在整个网站上保持一致,例如全局导航。 Right now I have two separate files for this (one in each side) and I would like to only have one. 现在,我为此有两个单独的文件(每侧一个),我只想拥有一个。 This is hard to maintain because each time I change one I have to change the other (if I remember to), it breaks the DRY rule. 这很难维护,因为每次我更改一个都必须更改另一个(如果记得),这会违反DRY规则。 But if I try to include a file from one side to the other using a relative path it tells me the file path is not allowed because it goes outside the document root. 但是,如果我尝试使用相对路径从一侧到另一侧包含一个文件,它会告诉我该文件路径是不允许的,因为它超出了文档根目录。 If I try to include it with an absolute URL I get another error saying file access is disabled in the server configuration. 如果尝试将其包含在绝对URL中,则会出现另一个错误,指出服务器配置中的文件访问已禁用。 (I am on a hosting account so the most I can change as far as server config is limited to .htaccess). (我使用的是托管帐户,因此只要服务器配置限制为.htaccess,我最多可以更改)。 So can anyone think of a work-around for this? 那么,有人可以考虑解决此问题的方法吗?

您是否有能力在两个目录之间创建符号链接

Why not put your global include file in yet another directory (lets call it library ) and then have each http root have an include file that includes ../library/lib.php , then sets specific paramaters. 为什么不将全局包含文件放在另一个目录中(我们称其为library ),然后让每个http根目录都包含一个包含文件../library/lib.php ,然后设置特定的参数。 This gives you the added benifit of your library php files not being in the document root path as well. 这为您提供了库php文件的额外好处,该好处也不在文档根目录中。

And actually. 实际上。 Updating because I just read the entry about "relative path" issues. 正在更新,因为我刚刚阅读了有关“相对路径”问题的条目。

Could you set the "include path" php value to include that directory? 您可以设置“ include path” php值来包含该目录吗?

Something like this: 像这样:

ini_set('include_path', realpath(dirname(__FILE__)."/../library").":".ini_get('include_path'));
require_once('lib.php');

Did a little more research - seems that changing open_basedir is not possible unless you are able to edit the httpd.conf or php.ini values. 做了更多的研究-除非您能够编辑httpd.conf或php.ini值,否则似乎无法更改open_basedir PHP Manual: open_basedir PHP手册:open_basedir

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM