[thelist] Getting path minus document root using PHP

Vijay Immanuel codelad at gmail.com
Fri Dec 14 00:13:41 CST 2007

On Dec 14, 2007 1:04 AM, Stephen Rider <evolt_org at striderweb.com> wrote:
> I have a string that contains the server path of a file within the
> site document directory.  I want to extract that string _minus_ the
> document root path, which makes up the beginning of it.
> Can someone tell me the best way to do this with PHP?
> Example:
> $string = "/sites/mysite.com/public_html/subdirectory/myfile.php"
> I want:
> $newstring = "/subdirectory/myfile.php"

Only subtly different from an earlier solution:

    $string = '/sites/mysite.com/public_html/subdirectory/myfile.php';
// or I've sometimes used $_SERVER['SCRIPT_FILENAME'], if string ==
path of current script

    $newstring = substr($string, strlen($_SERVER['DOCUMENT_ROOT']));


More information about the thelist mailing list