Home >php教程 >php手册 >解决PHP相对目录问题

解决PHP相对目录问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 11:35:561137browse

  用include或requice包含文件时,文件路径前都加上dirname(__file__)。例如:现有a.php、b.php、c.php、d.php4个文件,他们关系为:

    b.php(与根目录同一级)
    根目录/a.php
    根目录/c.php 
    根目录/dir/d.php

    现在a.php中要包含b.php、c.php。写法如下:

include_once(dirname(__file__)."/../b.php");
include_once(dirname(__file__)."/c.php");
?>
    如果c.php中要包含d.php。写法如下:
include_once(dirname(__file__)."/dir/d.php");
?>
    此方式在目前来说是解决路径错位最有效的方法
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