NYCPHP Meetup

NYPHP.org

[nycphp-talk] Check for absolute path (cross platform)

Jakob Buchgraber jakob.buchgraber at googlemail.com
Mon Sep 10 11:57:14 EDT 2007


I wrote a function that searches for a file within the include_path.
Here it is:

function ppcIncludize ($path) {

     // check for absolute path
     if ($path[0] == "/" || $path[1] == ":") {
         if (file_exists ($path)) {
             return $path;
         }

         return false;
     }

     $includePath = get_include_path();

     foreach (explode (PATH_SEPARATOR, $includePath) as $directory) {
         $tmpPath = "$directory/$path";

         if (file_exists ($tmpPath)) {
             return $tmpPath;
         }
     }

     return false;
}

This function only makes sense if the parameter given is a relative path 
to a file or folder and as this is not always the case I first check if 
it's an absolute path. So for unix systems it's quite easy, just need to 
   check for / at the beginning, but on windows I am not sure as I don't 
use and have never developed under Windows. However as far as I know a 
absolute path starts with a drive letter e.g. C followed by a :. So I 
just check for the : . I am not a 100% sure if this works, so may 
anybody test it for me or at least tell me if I am write with my 
assumption about windows path names.

Thanks!

Jakob
-- 
Sun Certified Programmer for the Java 2 Platform, Standard Edition 5.0



More information about the talk mailing list