PHP display current server path

后端 未结 6 1017
春和景丽
春和景丽 2020-12-29 20:28

I need to setup a path in my php but I currently don\'t know the path.

I need to configure the paths to the uploads directory

Should look like this below:

相关标签:
6条回答
  • 2020-12-29 21:07

    If you call getcwd it should give you the path:

    <?php
      echo getcwd();
    ?>
    
    0 讨论(0)
  • 2020-12-29 21:08

    php can call command line operations so

    echo exec("pwd");
    
    0 讨论(0)
  • 2020-12-29 21:17
    • To get your current working directory: getcwd() (documentation)
    • To get the document root directory: $_SERVER['DOCUMENT_ROOT']
    • To get the filename of the current script: $_SERVER['SCRIPT_FILENAME']
    0 讨论(0)
  • 2020-12-29 21:18

    here is a test script to run on your server to see what is reliabel.

    <?php
    $host = gethostname();
    $ip = gethostbyname($host);
    echo "gethostname and gethostbyname: $host at $ip<br>";
    $server = $_SERVER['SERVER_ADDR'];
    echo "_SERVER[SERVER_ADDR]: $server<br>";
    $my_current_ip=exec("ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'");
    echo "exec ifconfig ... : $my_current_ip<br>";
    $external_ip = file_get_contents("http://ipecho.net/plain");
    echo "get contents ipecho.net: $external_ip<br>";
    ?>
    

    The only different option in there is using fiel_get_contents rather than curl for the extrernal website lookup.

    This is the result of hitting the web page on a shared hosting, free account. (actual server name and IP changed)

    gethostname and gethostbyname: freesites.servercluster.com at 345.27.413.51
    _SERVER[SERVER_ADDR]: 127.0.0.7
    exec ifconfig ... :
    get contents ipecho.net: 345.27.413.51
    

    Why needed this? Decided to point A record at server to see if it opens the web page. Later ran script to save ip and update on ghost site on same server to lookup IP and alert if changed.

    In this case, good results optained by:

    gethostname() & 
    gethostbyname($host)
    or 
    file_get_contents("http://ipecho.net/plain")
    
    0 讨论(0)
  • 2020-12-29 21:29
    echo $_SERVER["DOCUMENT_ROOT"];
    

    'DOCUMENT_ROOT' The document root directory under which the current script is executing, as defined in the server's configuration file.

    http://php.net/manual/en/reserved.variables.server.php

    0 讨论(0)
  • 2020-12-29 21:30

    You can also use the following alternative realpath.

    Create a file called path.php

    Put the following code inside by specifying the name of the created file.

    <?php 
        echo realpath('path.php'); 
    ?>
    

    A php file that you can move to all your folders to always have the absolute path from where the executed file is located.

    ;-)

    0 讨论(0)
提交回复
热议问题