Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Different ways to find paths via PHP
<?php
/**
* Defined paths for local/production vs. staging servers
* example: http://website.com/main-folder/sub-folder/paths.php
* $full_uri: http://website.com/main-folder/sub-folder/paths.php
*/
$full_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$path = dirname($_SERVER[PHP_SELF]);
$current_path = substr( $path, strrpos($path,'/') + 1 );
if( $_SERVER['HTTP_HOST'] === 'dev.cmn.com' ) {
define( 'ASSETS_PATH', '/xavier' );
define( 'DOMAIN_PATH', 'http://' . $_SERVER['HTTP_HOST'] . ASSETS_PATH );
define( 'BASE_PATH', $_SERVER['DOCUMENT_ROOT'] . ASSETS_PATH );
} else {
define( 'ASSETS_PATH', '' );
define( 'DOMAIN_PATH', 'http://' . $_SERVER['HTTP_HOST'] );
define( 'BASE_PATH', $_SERVER['DOCUMENT_ROOT'] );
}
// Enable PHP error reporting
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
?>
<?php echo '<strong>dirname(dirname(__FILE__)):</strong> ' . dirname(dirname(__FILE__)); ?>
<hr>
<?php echo '<strong>$_SERVER[\'DOCUMENT_ROOT\']:</strong> ' . $_SERVER['DOCUMENT_ROOT']; ?>
<hr>
<?php echo '<strong>http:// . $_SERVER[\'HTTP_HOST\'] . $_SERVER[\'REQUEST_URI\']</strong> ' . $full_uri; ?>
<hr>
<?php echo '<strong>$_SERVER[\'HTTP_HOST\']</strong> ' . $_SERVER['HTTP_HOST']; ?>
<hr>
<?php echo '<strong>ASSETS_PATH</strong> ' . ASSETS_PATH; ?>
<hr>
<?php echo '<strong>DOMAIN_PATH</strong> ' . DOMAIN_PATH; ?>
<hr>
<?php echo '<strong>BASE_PATH</strong> ' . BASE_PATH; ?>
<?php //include BASE_PATH . '/includes/content-header.php';?>
<?php
/**
* example: http://website.com/main-folder/sub-folder/paths.php
* output: PATH_INFO: ../../
*/
if (!defined("BASE_PATH")) define('BASE_PATH', dirname($_SERVER['SCRIPT_NAME'])=='/' ? './' : str_repeat("../", substr_count(dirname($_SERVER["SCRIPT_NAME"]), "/")));
echo "PATH_INFO: " . BASE_PATH . "<br />";
?>
<?php
/**
* example: http://website.com/main-folder/sub-folder/paths.php
* output: CURRENT_PATH: sub-folder
*/
function current_dir() {
$path = dirname($_SERVER[PHP_SELF]);
$position = strrpos($path,'/') + 1;
print substr($path,$position);
}
echo "CURRENT_PATH: "; current_dir(); echo "<br />";
?>
<?php
/**
* example: http://website.com/main-folder/sub-folder/paths.php
* output: DOCUMENT_ROOT: /var/www/main-folder/sub-folder/
*/
define('DOCUMENT_ROOT', dirname(realpath(__FILE__)).'/');
echo "DOCUMENT_ROOT: " . DOCUMENT_ROOT . "<br />";
?>
<?php
/**
* example: http://website.com/main-folder/sub-folder/paths.php
* output: ROOT: /var/www/main-folder/sub-folder/
* output: HTTP: http://your_site_name.com/
*/
define("ROOT", __DIR__ ."/");
define("HTTP", ($_SERVER["SERVER_NAME"] == "localhost")
? "http://somewebsitename.com/"
: "http://your_site_name.com/"
);
print 'ROOT: ' . ROOT . "<br />";
print 'HTTP: ' . HTTP . "<br />";
?>
<?php
/**
* example: http://website.com/main-folder/sub-folder/paths.php
* output: PROJECT_ROOT: /var/www/main-folder/sub-folder
*/
define('PROJECT_ROOT', getcwd());
print 'PROJECT_ROOT: ' . PROJECT_ROOT . "<br />";
?>
<?php
/**
* example: http://website.com/main-folder/sub-folder/paths.php
* output: BASE_PATH_2: /var/www/main-folder/sub-folder
*/
define('BASE_PATH_2', str_replace('/include', '', dirname(__FILE__)));
print 'BASE_PATH_2: ' . BASE_PATH_2 . "<br />";
?>
<?php
/**
* example: http://website.com/main-folder/sub-folder/paths.php
* example output: BASE_URL:
*/
$base_url = str_replace($DOCUMENT_ROOT, "", dirname($PHP_SELF));
print 'BASE_URL: ' . $base_url . "<br />";
echo $base_url;
?>
<?php
/**
* example: http://website.com/main-folder/sub-folder/paths.php
* example output: PUBLIC_BASE:
*/
function public_base_directory() {
//get public directory structure eg "/top/second/third"
$public_directory = dirname($_SERVER['PHP_SELF']);
//place each directory into array
$directory_array = explode('/', $public_directory);
//get highest or top level in array of directory strings
$public_base = max($directory_array);
return $public_base;
}
echo "PUBLIC_BASE: "; public_base_directory(); echo "<br />";
?>
<?php
/**
* example: http://website.com/main-folder/sub-folder/paths.php
* output: sub-folder
*/
$parentparentdir = basename( dirname(__FILE__) );
echo 'PARENT_DIR: ' . $parentparentdir;
?>
<?php
/**
* example: http://website.com/main-folder/sub-folder/paths.php
* output: $_SERVER[PHP_SELF]: /main-folder/sub-folder/paths.php
* output: Dirname($_SERVER[PHP_SELF]: /main-folder/sub-folder
*/
echo '$_SERVER[PHP_SELF]: ' . $_SERVER['PHP_SELF'] . '<br />';
echo 'Dirname($_SERVER[PHP_SELF]: ' . dirname($_SERVER['PHP_SELF']) . '<br>';
?>
<?php
/**
* example: http://website.com/main-folder/sub-folder/paths.php
* output: BASE_URL: /main-folder/
*/
$string = preg_replace('%/main-folder/?(.+)?%', '/main-folder/', dirname($_SERVER['PHP_SELF']));
define( 'BASE_URL', preg_replace('%/main-folder/?(.+)?%', '/main-folder/', $string) );
echo 'BASE_URL: ' . BASE_URL . '<br>';
?>
<?php
/**
* example: http://website.com/main-folder/sub-folder/paths.php
* $full_uri: http://website.com/main-folder/sub-folder/paths.php
* $base_uri: http://website.com/main-folder/
* FULL_PATH: /main-folder/sub-folder/
* BASE_PATH: /main-folder/
*/
$full_uri = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo '$full_uri: ' . $full_uri . '<br>';
$base_uri = preg_replace( '%/main-folder/?(.+)?%', '/main-folder/', $full_uri );
echo '$base_uri: ' . $base_uri . '<br>';
define( 'FULL_PATH', dirname($_SERVER['PHP_SELF']) . '/' );
echo 'FULL_PATH: ' . FULL_PATH . '<br>';
define( 'BASE_PATH_3', preg_replace( '%/main-folder/?(.+)?%', '/main-folder/', FULL_PATH ) );
echo 'BASE_PATH_3: ' . BASE_PATH_3 . '<br>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment