如何获取从中调用PHP脚本的目录 [英] How to get directory where PHP script was called from

查看:49
本文介绍了如何获取从中调用PHP脚本的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某个目录中有一个PHP脚本。当我从命令行调用它时,getcwd(), __ DIR __ 和exec( pwd)返回脚本本身的目录,而不是pwd。

I have a PHP script in some directory. When I call it from the command line, getcwd(), __DIR__ and exec("pwd") return the directory of the script itself, not pwd.

例如从/ tmp调用/some/place/test.php:

E.g. calling /some/place/test.php from /tmp:

echo(getcwd());        // prints /some/place

echo(exec("pwd"));     // prints /some/place

echo(__DIR__);         // prints /some/place

echo($_SERVER["PWD"]); // prints /tmp

$ _ SERVER [ PWD]具有我需要的值,但是在文档http://php.net/manual/zh/reserved.variables .server.php

$_SERVER["PWD"] has the value that I need, but it's not mentioned in docs http://php.net/manual/en/reserved.variables.server.php

推荐答案

几乎打了 Post问题,并猜测要检查命令行参数。 :-)

Almost hit the "Post question" and guessed to check command line parameters. :-)

$ php -h
...
  -C               Do not chdir to the script's directory
...

这篇关于如何获取从中调用PHP脚本的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆