Skip to content

Instantly share code, notes, and snippets.

@jasdeepkhalsa
Last active January 25, 2016 14:44
Show Gist options
  • Save jasdeepkhalsa/5265176 to your computer and use it in GitHub Desktop.
Save jasdeepkhalsa/5265176 to your computer and use it in GitHub Desktop.
Get the current file's public directory path (URL) in PHP
<?php
function get_current_dir(){
$file = realpath(dirname(__FILE__)); // Current file path
$patt = '/\/path\/to\/public\/directory/'; // Regex pattern for all non-public directories
$url = preg_replace($patt,'', $file); // Take out those non-public directories
return $url . '/'; // Return the URL with a trailing slash
}
/**
* This function may be useful for occasions where you need to call a file in PHP included in the same directory
* For example main.php and json.php are in the same directory on the server: /username/domain.co.uk/public/files/
* Inside main.php you would like to point to json.php using its URL domain.co.uk/json.php for an AJAX call
* This function will return the relative path string '/files/' to which the file string 'json.php' can just be appended
* Then if the files are moved elsewhere in the public folder, they will continue to be referenced correctly,
* so long as the path to the non-public directories does not change
*/
?>
<?php
// Modified from: http://stackoverflow.com/questions/6768793/get-the-full-url-in-php
// For more examples see http://stackoverflow.com/questions/6768793/get-the-full-url-in-php
function getMyUrl($use_forwarded_host=false)
{
$ssl = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? true:false;
$sp = strtolower($_SERVER['SERVER_PROTOCOL']);
$protocol = substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : '');
$port = $_SERVER['SERVER_PORT'];
$port = ((!$ssl && $port=='80') || ($ssl && $port=='443')) ? '' : ':'.$port;
$host = ($use_forwarded_host && isset($_SERVER['HTTP_X_FORWARDED_HOST'])) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null);
$host = isset($host) ? $host : $_SERVER['SERVER_NAME'] . $port;
return $protocol . '://' . $host . $_SERVER['REQUEST_URI'];
}
// Outputs e.g. http://main.xfiddle.com/code_10692803.php?a=b&c=d
?>
<?php
// FROM: https://github.com/symfony/Routing/blob/master/Generator/UrlGenerator.php
/**
* Returns the target path as relative reference from the base path.
*
* Only the URIs path component (no schema, host etc.) is relevant and must be given, starting with a slash.
* Both paths must be absolute and not contain relative parts.
* Relative URLs from one resource to another are useful when generating self-contained downloadable document archives.
* Furthermore, they can be used to reduce the link size in documents.
*
* Example target paths, given a base path of "/a/b/c/d":
* - "/a/b/c/d" -> ""
* - "/a/b/c/" -> "./"
* - "/a/b/" -> "../"
* - "/a/b/c/other" -> "other"
* - "/a/x/y" -> "../../x/y"
*
* @param string $basePath The base path
* @param string $targetPath The target path
*
* @return string The relative target path
*/
public static function getRelativePath($basePath, $targetPath)
{
if ($basePath === $targetPath) {
return '';
}
$sourceDirs = explode('/', isset($basePath[0]) && '/' === $basePath[0] ? substr($basePath, 1) : $basePath);
$targetDirs = explode('/', isset($targetPath[0]) && '/' === $targetPath[0] ? substr($targetPath, 1) : $targetPath);
array_pop($sourceDirs);
$targetFile = array_pop($targetDirs);
foreach ($sourceDirs as $i => $dir) {
if (isset($targetDirs[$i]) && $dir === $targetDirs[$i]) {
unset($sourceDirs[$i], $targetDirs[$i]);
} else {
break;
}
}
$targetDirs[] = $targetFile;
$path = str_repeat('../', count($sourceDirs)).implode('/', $targetDirs);
// A reference to the same base directory or an empty subdirectory must be prefixed with "./".
// This also applies to a segment with a colon character (e.g., "file:colon") that cannot be used
// as the first segment of a relative-path reference, as it would be mistaken for a scheme name
// (see http://tools.ietf.org/html/rfc3986#section-4.2).
return '' === $path || '/' === $path[0]
|| false !== ($colonPos = strpos($path, ':')) && ($colonPos < ($slashPos = strpos($path, '/')) || false === $slashPos)
? "./$path" : $path;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment