SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Budapest
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    dirname(__FILE__) help needed

    Hi,

    My aim: to return ONLY the parent directory of a given file, for example:

    from '/htdocs/sales/products/page.php' I want to return 'products'

    As a beginner I have found dirname(__FILE__) and getenv() but am not sure how to extract only the parent. Any other better solutions would also be welcome.

    TIA

    Fred

  2. #2
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem with dirname (and anything else from pathinfo) is that it returns the whole path and it isn't broken down. There are many ways to do this - scanning the path for the final /, using a regexp to extract it, or something like this which will work fine for you:

    PHP Code:
    $dir '/htdocs/sales/products/page.php';

    $dir_sections explode('/'$dir);
    if (
    is_array($dir_sections))
    {
        
    array_pop($dir_sections);
        
    $parent end($dir_sections);
        echo 
    $parent;

    Cheers,

    Rich
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.

  3. #3
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Budapest
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that, it works! I want it to be dynamic (working through templates in DW) so is it safe to change the first line to:

    $dir = getenv(SCRIPT_NAME);

    ?

  4. #4
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure, I just set it to be the same as your example.
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.


Bookmarks

Posting Permissions

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