PHP 动态包含基于当前文件路径

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

PHP Dynamic include based on current file path

phpdynamicincludefilenames

提问by Adrian M.

I want to find a method to include some files based on the current file path.. for example:

我想找到一种方法来包含基于当前文件路径的一些文件.. 例如:

I have "website.com/templates/name1/index.php", this "index.php should be a unique file that I will use in many different directories on different depths, so I want to make the code universal so I don't need to change anything inside this file..

我有“website.com/templates/name1/index.php”,这个“index.php 应该是一个唯一的文件,我将在不同深度的许多不同目录中使用它,所以我想让代码通用,所以我不这样做”不需要更改此文件中的任何内容..

So if this "index.php" is located in

所以如果这个“index.php”位于

"website.com/templates/name1/index.php"

website.com/templates/name1/index.php”

than it should include the file located here:

它应该包含位于此处的文件:

"website.com/content/templates/name1/content.php"

website.com/content/templates/name1/content.php”

another example:

另一个例子:

"website.com/templates/name2/index.php"

website.com/templates/name2/index.php”

than it should include the file located here:

它应该包含位于此处的文件:

"website.com/content/templates/name2/content.php"

website.com/content/templates/name2/content.php”

Also I want to overrun "Warning: include_once() [function.include-once]: http:// wrapper is disabled in the server configuration by allow_url_include=0" kind of error.. because is disabled and unsafe..

另外我想超出“警告:include_once() [function.include-once]:http://wrapper is disabled in the server configuration by allow_url_include=0”类型的错误..因为被禁用且不安全..

Is there a way to achieve this? Thank you!

有没有办法实现这一目标?谢谢!

回答by Sergey Kuznetsov

I think you need to use __FILE__(it has two underscores at the start and at the end of the name) and DIRECTORY_SEPARATORconstants for working with files based on the current file path.

我认为您需要使用__FILE__(它在名称的开头和结尾有两个下划线)和DIRECTORY_SEPARATOR常量来处理基于当前文件路径的文件。

For example:

例如:

<?php
  // in this var you will get the absolute file path of the current file
  $current_file_path = dirname(__FILE__);
  // with the next line we will include the 'somefile.php'
  // which based in the upper directory to the current path
  include(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'somefile.php');

Using DIRECTORY_SEPARATORconstant is more safe than using "/"(or "\") symbols, because Windows and *nix directory separators are different and your interpretator will use proper value on the different platforms.

使用DIRECTORY_SEPARATOR常量比使用“/”(或“\”)符号更安全,因为 Windows 和 *nix 目录分隔符是不同的,您的解释器将在不同平台上使用正确的值。

回答by vignesh

<?php   
if (isset($_GET['service'])) {
    include('subContent/serviceMenu.html');
} else if (isset($_GET['business'])) {
    include('subContent/business_menu.html');
} else if (isset($_GET['info'])) {
    include('subContent/info_menu.html');
}
else {
    include('subContent/banner.html');
}
?>
<a href="http://localhost/yourpage.php?service" />

回答by Rasmus

I'd just do something as simple as:

我只会做一些简单的事情:

$dir = str_replace('website.com/','website.com/content/',__DIR__);
include "$dir/content.php";

And your HTTP wrapper issue doesn't seem to have anything to do with this. What do you mean by overrun it? You generally never want to do a remote include.

而您的 HTTP 包装器问题似乎与此无关。超过它是什么意思?您通常永远不想进行远程包含。

回答by Ashesh Ambasta

Why not do it in the simple way:

为什么不以简单的方式做到这一点:

dirname(__FILE__); //Current working directory
dirname(dirname(__FILE__)); //Get path to current working directory

And then finally

然后最后

$include_path = $dir_path . 'file_to_include.php';
include $include_path;