如何找到我的htdocs目录。
执行以下命令时:
echo dirname(__FILE__);
我得到以下输出:
/var/www/myexample.com/htdocs/required/css
我想找到路径直到/ htdocs,但我不想硬编码的东西
我知道我可以使用:
dirname(dirname(dirname(__FILE__)))
但是,如果当前文件位于“css”目录中,那将会有效。如果我将移动到“css”目录中的其他目录,那么它将无法工作。
对此有何解决方案?
答案 0 :(得分:6)
答案 1 :(得分:2)
在大多数设置中$_SERVER['DOCUMENT_ROOT']
应该可以提供您想要的内容。
答案 2 :(得分:0)
如果$ _SERVER ['document_root']不可用,我会使用此脚本查找它们,包括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>";
?>