,如果我有2个脚本:PHP文件包含在目录中的绝对链接
includes/ajax_dir/script_1.php
和
includes/script_2.php
从script_1.php
我知道我可以包括srcipt_2.php
像这样:
include("../script_2.php");
然而,我怎么能做到这一点,包括使用绝对链接?...
,如果我有2个脚本:PHP文件包含在目录中的绝对链接
includes/ajax_dir/script_1.php
和
includes/script_2.php
从script_1.php
我知道我可以包括srcipt_2.php
像这样:
include("../script_2.php");
然而,我怎么能做到这一点,包括使用绝对链接?...
dirname(__FILE__)."/../"
将工作的伟大,我认为。
我用dirname(__FILE__)."/somepath/
之所以处处是:
想象一下,你有这样的文件夹结构
//index.php
<?php
include("includes/include_libs.php");
// /includes/include_libs.php
<?php
include("includes/db.php"); //here is the problematical part
// /ajax/dostuff.php
<?php
include("../includes/include_libs.php"); /**
this will cause that include_libs tries to include /ajax/includes/db.php instead of /includes/db.php, to fix this trouble, change the problematical line in includes_libs.php to
include(dirname(__FILE__)."/db.php");
which will ensure that real db.php will be called */
喜欢的东西:
include(realpath(dirname(__FILE__)."../script_2.php"));
感谢lkke,即工作然而这也给了一个错误:[function.dir]:未能打开目录:在...还有什么会做这种方式的优势,因为我还需要知道不是一个目录相对路径......当然,这比仅仅执行include(“../ script_2.php”)要慢。感谢您的帮助。 – jon
,可以使用绝对文件系统路径,即在/ var/WWW /htdocs/your.php,但不能使用绝对URL,如http://your.domain/yourscript.php。 –