I was searching for a way to do this myself, and came up with this method:
--------------------------------------------------------------------------

```
<?php
$script_name = $_SERVER['SCRIPT_NAME']; // to get www.myurl.com

// create a constant out of the server_name
define('HTTP_HOST', $_SERVER['HTTP_HOST'].'/');

$path_parts = pathinfo($script_name); // returns an array with path names

// get the filename with the extension
$file_name = $path_parts['filename'] . '.' . $path_parts['extension'];
// get the directory which the script lives in
$dir = rtrim($script_name, $file_name);
// trim of the left slash since it was added on the end of the HTTP_HOST constant
$dir = ltrim($dir, '/');
// store the server name and directory paths in a variable
$dir = HTTP_HOST.$dir;

echo $dir; // will return f.ex. "www.myurl.com/dir1/dir2/dir3/"

```

Lets say I have a file called "testfile.php" which lives under directory "dir3" at "`www.myurl.com/dir1/dir2/dir3`", so the full url would be "`www.myurl.com/dir1/dir2/dir3/testfile.php`".

If I declare a constant for the URL plus full directory path in my paths file, or some configuration file which I include in f.ex my index file, and call that constant inside testfile.php end echo it. It will echo out "`www.myurl.com/dir1/dir2/dir3/`".

### My question is, is this method acceptable or is there a better way to achieve the URL with directory path to a given file?

Thank you.