Get web path

Discussion in 'PHP' started by fireworld2406, Apr 4, 2009.

  1. #1
    Hello---

    This is probably a very newbish question, so please bear with me.

    How can I get the "web directory" of the program?

    ie. If I have a script running in "localhost/test/inc/test.php", how can I retrieve the value "localhost/test"?

    I hope that makes sense. Please let me know if it doesn't, or if you need anymore information.

    Thank you in advance for your help!
     
    fireworld2406, Apr 4, 2009 IP
  2. kusal

    kusal Peon

    Messages:
    91
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try

    echo $_SERVER["SCRIPT_NAME"];

    this will output something like this

    test/inc/test.php

    you will need to use explode function get the correct information form the output
     
    kusal, Apr 4, 2009 IP
  3. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #3
    If you've opened /includes/system/config.php, it will say that your path is /includes/system/ :
    <?php
    function getScriptPath() {
         $self = $_SERVER['PHP_SELF'];
         preg_match('/(.*)\/[a-zA-Z0-9.php]/', $self, $path);
         $full_path = $path[1] . "/";
         return $full_path;
    }
    ?>
    PHP:
    Another easy way to get your current file & path :
    <?php
    $file_and_path = __FILE__;
    echo $file_and_path;
    ?>
    PHP:
     
    ActiveFrost, Apr 5, 2009 IP
  4. spikyy

    spikyy Peon

    Messages:
    147
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    using __FILE__ it's the easiest method.
     
    spikyy, Apr 5, 2009 IP
  5. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #5
    And once you have __FILE__ , dirname() will strip a file/directory name off the right side.

    e.g.,
    echo dirname(dirname(__FILE__));
    Code (markup):
    will get you what you're after in this case.
     
    SmallPotatoes, Apr 5, 2009 IP
  6. fireworld2406

    fireworld2406 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thank you everyone for your suggestions! However, I didn't quite type what I wanted right :eek: ( I was really tired..... :) )

    I am looking for something where if the script is running in "http://localhost/test/inc/test.php", to get the "http://localhost/test" part. (I left out the http:// )

    Any ideas on how to get that?

    Once again, thank you all for your help!
     
    fireworld2406, Apr 5, 2009 IP
  7. fireworld2406

    fireworld2406 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I got it. Thank you to everyone for their help.

    I modified ActiveFrost's script a slight bit. Here is my new script for anyone who's interested:
    <?php
    function getScriptPath() {
    $self = explode("/",$_SERVER['PHP_SELF']);
    echo $_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT']."/".$self[1];
    }

    getScriptPath();
    ?>

    Thank you everyone! You have been so helpful!
     
    fireworld2406, Apr 5, 2009 IP