2011-12-17 56 views
-1

,如果我有2个脚本:PHP文件包含在目录中的绝对链接

includes/ajax_dir/script_1.php 

includes/script_2.php 

script_1.php我知道我可以包括srcipt_2.php像这样:

include("../script_2.php"); 

然而,我怎么能做到这一点,包括使用绝对链接?...

+0

,可以使用绝对文件系统路径,即在/ var/WWW /htdocs/your.php,但不能使用绝对URL,如http://your.domain/yourscript.php。 –

回答

1

dirname(__FILE__)."/../"将工作的伟大,我认为。

我用dirname(__FILE__)."/somepath/之所以处处是:

想象一下,你有这样的文件夹结构

  • /
    • 的index.php
    • /包括
      • /包括/ include_libs。 PHP
      • /inclu DES/db.php中
  • /AJAX
    • dostuff.php

//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 */ 
+0

不错...这是最好使用上述相对链接......如果是这样,为什么..亲切的问候J. – jon

+0

@乔恩:??因为这可确保你总是有正确的文件,不管你的CWD是什么。 –

+0

感谢马丁......但林不知道我理解,因为我不能看的时候会有,相对链接会拉错了文件的情况下......不过我只是把它作为它始终是最好使用绝对路径...感谢你在回答问题时帮助...关心Ĵ – jon

2

喜欢的东西:

include(realpath(dirname(__FILE__)."../script_2.php")); 
+0

感谢lkke,即工作然而这也给了一个错误:[function.dir]:未能打开目录:在...还有什么会做这种方式的优势,因为我还需要知道不是一个目录相对路径......当然,这比仅仅执行include(“../ script_2.php”)要慢。感谢您的帮助。 – jon

相关问题