How do I set an absolute include path in PHP?

后端 未结 10 1895
别那么骄傲
别那么骄傲 2020-12-04 11:32

In HTML, I can find a file starting from the web server\'s root folder by beginning the filepath with "/". Like:

/images/some_image         


        
10条回答
  •  执笔经年
    2020-12-04 11:55

    I follow Wordpress's example on this one. I go and define a root path, normally the document root, and then go define a bunch of other path's along with that (one for each of my class dirs. IE: database, users, html, etc). Often I will define the root path manually instead of relying on a server variable.

    Example

    
    if($_SERVER['SERVERNAME'] == "localhost")
    {
        define("ABS_PATH", "/path/to/upper/most/directory"); // Manual
    }
    else
    {
        define("ABS_PATH, dirname(__FILE__));
        // This defines the path as the directory of the containing file, normally a config.php
    }
    
    // define other paths...
    
    include(ABS_PATH."/mystuff.php");
    
    

提交回复
热议问题