我想找到一种方法来包含基于当前文件路径的一些文件..例如:
我有“website.com/templates/name1/index.php”,这个“index.php应该是一个独特的文件,我将在不同深度的许多不同目录中使用它,所以我想让代码通用,所以我不”不需要更改此文件中的任何内容..
所以如果这个“index.php”位于
"网站.com/templates/name1/index.php"
它应该包含位于此处的文件:
"website.com/content/templates/name1/content.php"
另一个例子:
"网站.com/templates/name2/index.php"
它应该包含位于此处的文件:
"website.com/content/templates/name2/content.php”
我也想超越”警告:include_once() [function.include-once]:http:// 包装器在服务器配置中被allow_url_include=0 禁用“某种错误..因为被禁用且不安全..
有办法实现这一点吗?
谢谢你!
我认为你需要使用__FILE__(名称的开头和结尾有两个下划线)和目录分隔符用于根据当前文件路径处理文件的常量。
例如:
<?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 目录分隔符常量比使用更安全"/" (or "\") 符号,因为 Windows 和 *nix 目录分隔符不同,您的解释器将在不同平台上使用正确的值。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)