Home >Backend Development >PHP Tutorial >require_once needs to pay attention to the problem and set the include path

require_once needs to pay attention to the problem and set the include path

黄舟
黄舟Original
2017-06-25 11:26:512278browse

Set common files that need to be included: (in the project root directory)

include.php

<?php
    header("content-type:text/html;charset=utf-8");
    date_default_timezone_get("PRC");
    session_start();
    //_FILE_返回是当前代码所在文件(而不是url所在文件)完整(绝对)路径
    //dirname($file_name)返回$file_name所在层目录名
    define("ROOT",dirname(FILE));
    set_include_path(".".PATH_SEPARATOR.ROOT."/core".PATH_SEPARATOR.ROOT."/configs".PATH_SEPARATOR.get_include_path());
    require_once &#39;mysql.func.php&#39;;
    require_once &#39;image.func.php&#39;;
    require_once &#39;common.func.php&#39;;
    require_once &#39;string.func.php&#39;;
    require_once &#39;page.func.php&#39;;
    require_once &#39;configs.php&#39;;
    require_once &#39;admin.inc.php&#39;;
    require_once &#39;cate.inc.php&#39;;
    require_once &#39;pro.inc.php&#39;;
    require_once &#39;album.inc.php&#39;;
    require_once &#39;user.inc.php&#39;;
    require_once &#39;upload.func.php&#39;;
    connect();
?>


Then, other pages directly require_once(' ./include.php') //relative to the path of include.php

But problems may occur when files are nested and included

The following is a detailed explanation

Click to open the link

Therefore, to be safe, it is best to add dirname(FILE) at the beginning of

require_once (dirname(FILE).&#39;/&#39;.&#39;../include.php&#39;);

.'/'

The above is the detailed content of require_once needs to pay attention to the problem and set the include path. For more information, please follow other related articles on the PHP Chinese website!

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