问题描述
在CLI $_SERVER['DOCUMENT_ROOT']
中不起作用.我怎样才能解决这个问题?还有其他可用的选项吗?我不能使用相对路径,因为文件位于各个目录中.
In CLI $_SERVER['DOCUMENT_ROOT']
is not working. How can I fix this? Is there any other option available. I cannot use relative paths, because files are in various directories.
推荐答案
$ _ SERVER包含标头,这些标头在CLI中不可用. Web服务器定义文档根目录.在CLI中,您没有使用Web服务器,因此没有文档根目录.
$_SERVER contains headers which won't be available in the CLI. The web server defines the document root. In the CLI, you aren't using a web server, so there is no document root.
您可以尝试依赖环境变量,前提是它们是由Shell设置的.
You can try to rely on environmental variables, assuming they are set by your shell.
例如,PWD
代表当前目录,HOME
代表用户的主目录.
For instance, PWD
represents the current directory and HOME
represents the user's home directory.
$pwd = getenv('PWD');
$home = getenv('HOME');
您还可以使用__FILE__
或__DIR__
魔术常数 尝试描述您当前所在的路径.
You can also use __FILE__
or __DIR__
magic constants to try and depict the path you are currently in.
这篇关于CLI中的$ _SERVER文档根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!