How can I find my htdocs directory.
When I execute the following command:
echo dirname(__FILE__);
I get the following output:
/var/www/myexample.com/htdocs/required/css
I want to find path till /htdocs, but I don't want to hard-code the things
I know I can use:
dirname(dirnam开发者_开发百科e(dirname(__FILE__)))
But, that will work if the current file is in "css" directory. If I will move to some other directory inside "css" directory, then it will not work.
Any solution for this?
If you are running Apache, the
$_SERVER["DOCUMENT_ROOT"]
environment variable should give you the current document root. See $_SERVER in the manual.
On most setups $_SERVER['DOCUMENT_ROOT']
should give you what you want.
I use this script to find them all should $_SERVER['document_root'] be unavailable, including htdocs.
<?php
$scriptRelativeName = substr($_SERVER['SCRIPT_FILENAME'], strripos($_SERVER['SCRIPT_FILENAME'], $_SERVER['SCRIPT_NAME'])); //get relative name of script case sensitive from script_filename provided by filesystem by getting position of script_name in url and substracting the filesystem path
$scriptRelativePath = substr($scriptRelativeName , 0, strrpos($scriptRelativeName, "/")); //get relative path by subtracting everything after the last slash in the relative name
$rootFolder = str_ireplace($scriptRelativeName, "", $_SERVER['SCRIPT_FILENAME']); //subtract the scriptrelativename from script_filename to get the root folder
echo "Relative Script Name: " . $scriptRelativeName . "<br>";
echo "Relative Script Path: " . $scriptRelativePath . "<br>";
echo "Htdocs/Root Folder Path: " . $rootFolder . "<br>";
?>
精彩评论