Wednesday, March 16, 2005

PHP: Converting Filepath to URL Path for Current Directory

Finally sat down and put this together. Based on this:

junk at plaino dot com

The idea is that it will export seamlessly. But I haven't thoroughly checked the logic or tested it yet.

// URL path for current directory
$slash = '/';
$url_root = "http://".$_SERVER['HTTP_HOST'];
$self = $_SERVER['PHP_SELF'];
$self = str_replace('\\', $slash, $self);
$phys_path = getcwd();
$phys_path = str_replace('\\', $slash, $phys_path);

$file_path_array = explode ($slash, $self);
$file_name = array_pop($file_path_array);
$cwd_path_array = explode ($slash, $phys_path);
$self_dir_path_array = array_values (array_intersect ($file_path_array, $cwd_path_array));
$self_dir_path = implode("/", $self_dir_path_array)."/";

$cwd_url = $url_root . '/' . $self_dir_path;

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home