Skip to content

Instantly share code, notes, and snippets.

@peteboere
Created April 18, 2011 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peteboere/925177 to your computer and use it in GitHub Desktop.
Save peteboere/925177 to your computer and use it in GitHub Desktop.
Get a script's public URL relative to the domain
<?php
// Ensure document root superglobal contains no trailing slash
$_SERVER[ 'DOCUMENT_ROOT' ] = rtrim( $_SERVER[ 'DOCUMENT_ROOT' ], '\/' );
// Get current script directory
$this_dir = dirname( __FILE__ );
// Get a public, domain relative, path to current script directory
$this_public = substr( $this_dir, strlen( $_SERVER[ 'DOCUMENT_ROOT' ] ) );
// If substr returns false, point to the public root directory
$this_public = $this_public ? $this_public : '/';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment