Home  >  Article  >  Backend Development  >  PHP 包含文件路径有关问题

PHP 包含文件路径有关问题

WBOY
WBOYOriginal
2016-06-13 11:01:01602browse

PHP 包含文件路径问题

??? 最近刚刚在学PHP,在做一个小项目,用到了很多require和include,被绝对路径、相对路径弄的焦头烂额,最后终于搞明白了。

?

??? 1、php的相对路径是以运行的脚本为准,比如A包含B,B包含C,如果A、C在同一级目录,则B里包含C的路径应该是相对于A的,如果B、C在同一级目录,则B里包含C的路径是相对于C的。

??? 2、不管.php脚本怎么被包含,dirname(__FILE__)得到的都是脚本所在文件夹的绝对路径。


??? 这两条规则很难准确的运用,至少我自己就经常被搞晕,所以我用了一个比较取巧的办法,自己写了一个path.php,这样一来就很少出错了。


???
????? define('ROOT_PATH',dirname(__FILE__));
????? define('ACTION_PATH',ROOT_PATH.'/action/');
????? define('BEAN_PATH',ROOT_PATH.'/bean/');
????? define('DAO_PATH',ROOT_PATH.'/dao/');
????? define('IPADVIEW_PATH',ROOT_PATH.'/ipadView/');
????? define('VIEW_PATH',ROOT_PATH.'/view/');


??? ?>

?

?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn