通过 bat 文件在 Windows 上运行 php 脚本会在 require_once ($_SERVER['Document_Root']); 上返回错误
在 Windows XP 系统上,我有 test.bat
C:\Path\to\php.exe -f "C:\Path\to\test.php"
我还有 test.php
require_once ($_SERVER ['DOCUMENT_ROOT'] . '/Inc/Class/Connect_DB.php');
...更多代码。
当我在“CMD 模式”上执行 test.bat 时,它会返回致命错误,表示无法找到 require_once 文件。
相同的文件在浏览器上运行良好。它似乎无法识别bat文件中的$_SERVER
变量。 (我打算稍后通过schtasks.exe运行test.bat文件)
为什么这里不能读取$_SERVER
变量?
On Windows XP system, I have test.bat
C:\Path\to\php.exe -f "C:\Path\to\test.php"
I also have test.php
require_once ($_SERVER ['DOCUMENT_ROOT'] . '/Inc/Class/Connect_DB.php');
... more code.
When I execute test.bat on "CMD mode" it returns Fatal error saying it can't locate the require_once file.
The same file works fine on the browser. It seems that it can't recognize $_SERVER
variable on the bat file. (I plan to run test.bat file via schtasks.exe later on)
Why can't it read $_SERVER
variable here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
转储 $_SERVER 并检查是否设置了文档根目录。在我的安装中,$_SERVER 可从 cli 获取,但 DOCUMENT_ROOT 键设置为空字符串。即-> “”。
您最好通过使用以下内容来获取路径:
有很多方法可以做到这一点,但这应该给您一个想法。
快乐编码!
Dump $_SERVER and check if document root is set. On my install $_SERVER is available from cli, but the DOCUMENT_ROOT key is set to an empty string. ie -> "".
You would be better off getting the path by using something in the lines of:
There are many ways of doing this, but that should give you an idea.
Happy coding!
调用时,documentroot 为空
从 CLI USE "__DIR__" : http://php.net /manual/en/language.constants.predefined.php
documentroot is empty when called from CLI
USE "__DIR__" : http://php.net/manual/en/language.constants.predefined.php