Web Analytics Made Easy -
StatCounter Path + exclusion function - CodingForum


No announcement yet.

Path + exclusion function

  • Filter
  • Time
  • Show
Clear All
new posts

  • Path + exclusion function

    I have written a function to grab the system path of the scripts home directory.
    Often, the functions file is used from a directory deeper inside the script home directory, but we still want to return the main script directory.

    Example: Script is accessed from /home/public_html/ and then accessed from /home/public_html/install/ where we still want to return /home/public_html/ as that is the main script home directory.

    The function used is below, as you will notice i use strrpos() to return the position of the exclusion although supposedly, strings will not work below PHP 5. The server we tested on has PHP 4.4 installed with cPanel.

    I tested this before i relised strings were not supported and tested with 2 exclusions which returned different results.

    get_real_path("/install/"); would return /home/public_html/install while get_real_path("install/"); would return /home/public_html (as it should)

    As strings were not included below PHP 5, i am wondering why the second return should differ from the first or why it would even work at all.

    using get_real_path("install/"); is no problem, but i do not want to use it if it may cause problems on specific PHP installations.

    Could someone possibly shed some light on the situation or an alternative to my function?

    PHP Code:
    function get_real_path($exclusion=false)
    $cwd getcwd();
    $cwd str_replace"\\""/"$cwd );

        if (
    $exclusion !== false)
    // Get position of the exclusion
    $stop strrpos($cwd$exclusion);

            if (
    $stop === false)
    $stop strlen($cwd);    }
    $real_path substr$cwd0$stop );
    $real_path $cwd;

    // Remove last forward slash
    $check_last = @strrpos($real_path'/');
        if ( 
    $check_last+== @strlen($real_path) )
    $real_path substr($real_path0$check_last);

  • #2
    The deal is, strrpos() will use the first character of a string if it is passed a string (< php5). I don't think your function is dependable, since it won't know the difference between "install/" or "idiot/", and you probably want it to know the difference.


    • #3
      Ok, thanks that makes perfect sense.