Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Echo Current Directory

    Hey,

    Im just wondering how to echo the current directory of something without getting the whole url (/home/user/public_html/folder/). So basically if it is in my forum directory I just want to get "/forum/" without the full document root.

    Anyone know how? I tried REQUEST_URI but the problem is it echoes the whole url (include the filename).

    Thanks!
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    explode the REQUEST_URI to get the current directory

  • #3
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Okay... but say the current url is (/staff/panel/usercp.php) how do I manage that if the "usercp.php" could end up being different every time the page reloads?
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #4
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    PHP Code:
    $url $_SERVER['REQUEST_URI']; //returns URL
    $parts explode('/',$url);
    print_r($parts); 
    that should help you work things out

  • #5
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Alright... I'm lost now...

    I understand how the whole array thing works but I can't seem to figure out preg_replace() or preg_match() which I assume you would have to use to remove the file part.

    Sorry... anyone provide me with a working example?
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #6
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Scratch that, I've almost got it

    PHP Code:
    $url $_SERVER['REQUEST_URI'];
    $parts explode('/',$url);
    print_r($parts);
    echo 
    "<p>";
    if(
    preg_match('/.php/'$parts[2])){
        
    $final preg_replace('/(.*)(.php)(.*)/','',$parts[2]);
        echo 
    "/".$parts[0].$parts[1].$final."/";

    How can I get the preg_match to cycle through the whole array without writing it all out individually?
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #7
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Troy297 View Post
    how to echo the current directory of something without getting the whole url (/home/user/public_html/folder/).
    maybe getcwd();

  • #8
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Thxs for the suggestion but I've already tried, and I've finally perfected it, I think?

    PHP Code:
    $url $_SERVER['REQUEST_URI'];
    $parts explode('/',$url);
    foreach(
    $parts as $pa){
        if(
    preg_match('/(.php)/',$pa)){
            
    $final preg_replace('/(.*)(.php)(.*)/'''$pa);
            echo 
    $final."/";
        }else{
            echo 
    $pa;
        }

    Now the only thing is how can I piece everything back together into a viable location/folder dir? I need it in the form of a php variable.
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #9
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    ''$pa.''$ps;
    ??

  • #10
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Well the problem I now have is if I modify the code slightly to look like this:

    PHP Code:
    $url $_SERVER['REQUEST_URI'];
    $parts explode('/',$url);
    foreach(
    $parts as $pa){
        if(
    preg_match('/(.php)/',$pa)){
            
    $final preg_replace('/(.*)(.php)(.*)/'''$pa);
            
    $end $final;
        }else{
            
    $dir $pa;
        }
    }
    $folder "/".$dir.$end;
    echo 
    $folder
    It seems logical enough but what happens when the location is inside multiple folders, like this (forum/staff/install/)? How can I do that so that $dir won't just overwrite itself every time and then piece it all together at the end...?

    Thanks!
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #11
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Anyone help please?
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #12
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,084
    Thanks
    11
    Thanked 100 Times in 98 Posts
    you should have stuck with where rafiki was going which was going to be far easier.

    this will work on *nix, you would need to modify for win32
    PHP Code:
    <?
    $bits 
    explode('/'$_SERVER['SCRIPT_FILENAME']);
    echo 
    $bits[(count($bits)-2)];
    ?>
    & for the full path...
    <?
    $path 
    str_replace(basename($_SERVER['SCRIPT_FILENAME']),''$_SERVER['SCRIPT_FILENAME']);
    echo 
    $path;
    ?>
    Last edited by firepages; 08-08-2007 at 12:44 AM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #13
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Ya... that is alot easier - problem was I didn't know where rafiki was going

    Anyways... one last thing.... the first code snippet works great for what I need until you run into sub-level directories (mysite.com/v2/panel/staff/) - the code only shows the last one... not everything leading up to...

    I'll look up the count in the php manual to see if I can figure something out but does anyone know the "simple" fix?

    Thanks
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!

  • #14
    New Coder
    Join Date
    Aug 2007
    Location
    Wagga, Australia
    Posts
    59
    Thanks
    0
    Thanked 1 Time in 1 Post
    Here is an alternative I just came up with
    Code:
    <?php
    class dir {
      function dir() {
        $location = $_SERVER["SCRIPT_NAME"];
        list($this->dir) = split("[a-zA-Z0-9]*.php$", $location, 2);
      }
    }
    $directory = new dir();
    echo $directory->dir . "<br />";
    ?>
    basically it uses regular expressions to find a part in the script file path name that can consist of many letters and digits ending with .php and is at the end of the string, thus deleting the script name from the end of the string and you are left with the current directory. if u hav underscores or hyphens in ur scripts than add split("[a-zA-Z0-9_\-]*.php$", $location, 2) or it might be a front slash I cant remember what the cancellation symbol is for regex.
    Hope this has helped!
    (iv edited this post a couple of times because i had some problems with the code, like deleting a whole directory from the string if on of the folders was called php, but this script has been tested and works well now)
    Last edited by usik; 08-08-2007 at 01:06 PM.

  • #15
    Regular Coder Troy297's Avatar
    Join Date
    Oct 2006
    Location
    Earth
    Posts
    314
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Wow! Thanks so much!!!

    It works like a charm and does exactly what I wanted it to!!!

    Thanks again!! +Rep!
    Everyone hears what you say, friends listen to what you say, best friends listen to what you don't say.
    Radio DJ Panel v3 - It's Here!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •