2012-01-13 122 views
0
这里

超级noob问题...获取相对路径

文件来源: www.mysite.com/blah/blah/customdir/ajax/folder/file.php

,我想包括来源:

www.mysite.com/blah/blah/customdir/include/func/functions.php

所以我曾尝试:

require_once('.../include/func/functions.php'); 
require_once('../include/func/functions.php'); 
require_once('././include/func/functions.php'); 

和大量的其他变化。我如何得到它?在每个文件上设置顶级路径不是一个选项(这就是功能文件的用途...)

上面的每个给我一个错误: 警告:包括(././ include/func/functions.php)[function.include]:未能打开流:没有这样的文件或目录在/home/mysite/public_html/mysite.com/customdir/ajax/folder/index.php在线3

警告:包含(././ include/func/functions.php)[function.include]:无法打开流:/home/mysite/public_html/mysite.com/customdir/ajax/folder/中没有这样的文件或目录第3行的index.php

警告:include()[function.include]:无法打开'././ include /func/functions.php'(include_path ='。:/ usr/lib/php:/ usr/local/lib/php')位于/home/mysite/public_html/mysite.com/customdir/ajax/folder/第3行的index.php

回答

2

有很多方法可以包含文件。但是,似乎你正在尝试相对路径。在这种情况下,请尝试:

require_once '../../include/func/functions.php'; 

我鼓励你learn more about your options for including files。但简而言之,每个../都会带您回到一个目录。 ./代表当前目录。否则,你总是可以使用绝对路径,甚至可以使用混合动力车。

+0

它的工作原理。谢谢。为什么需要两倍的时间?瘸腿......试了很多东西。 – phpmeh 2012-01-13 19:58:10

+0

导致它是当前脚本上方的两个目录。查看更新。 – 2012-01-13 19:59:38

0

你在apache下运行吗?使用getenv(“DOCUMENT_ROOT”)并从那里设置你的路径。