How are paths calculated in PHP - Why are files in the current directory ignored?

前端 未结 3 1143
眼角桃花
眼角桃花 2021-01-22 21:31

Update:

The index.php file here:

/public_html/d/index.php

includes:

/public_html/d/core/source/class.File1.php
<         


        
3条回答
  •  -上瘾入骨i
    2021-01-22 22:13

    Are you using set_include_path anywhere in your app? Perhaps that could be the cause of your problem. By doing that you are specifying a list of directories where the require, include, fopen(), file(), readfile() and file_get_contents() functions look for files first.

    As an alternative I suggest you define the absolute path to avoid problems. Write this on your /public_html/d/core/source/class.File1.php

    // This if you are using PHP >= 5.3
    include(__DIR__ . '/class.File2.php');
    
    // Or this if you dont have the magic constant __DIR__ available
    include(dirname(__FILE__) . '/class.File2.php');
    

    __FILE__ and __DIR__ are magic constants. __FILE__ is a constant that has the absulute path to that specific file. And __DIR__ is the absulte path to the directory where that file is. You can even test them by doing echo __FILE__; or echo __DIR__;

    You can read more about them here http://php.net/manual/en/language.constants.predefined.php

提交回复
热议问题