Thursday, March 24, 2005

PHP cwd_to_url()

A revision to the code earlier posted -- added strtolower filtering. As a function:

/* fx cwd_to_url
converts current directory to URL path
author: tatwell_AT_gmail_DOT_com
******************************/

$slash = '/';

// standardize paths
$self = $_SERVER['PHP_SELF'];
$self = strtolower(str_replace('\\', $slash, $self));
$phys_path = getcwd();
$phys_path = strtolower(str_replace('\\', $slash, $phys_path));

// convert path strings to arrays
$file_path_array = explode ($slash, $self);
$cwd_path_array = explode ($slash, $phys_path);

// intersect arrays to get path trail
$self_dir_path_array = array_values (array_intersect ($file_path_array,
$cwd_path_array));
$self_dir_path = implode("/", $self_dir_path_array)."/";

// append path trail to url root
$url_path = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $self_dir_path;

// return
return $url_path;

} // end fx
/******************************/

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home