通过bat文件在windows上运行php脚本会在requireonce($_SERVER[';Document_


running php script on windows via bat file returns error on 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_one文件。

相同的文件在浏览器上运行良好。它似乎无法识别bat文件上的$_SERVER变量。(我计划稍后通过schtasks.exe运行test.bat文件)

为什么它不能在这里读取$_SERVER变量?

转储$_SERVER并检查是否设置了文档根目录。在我的安装中,$_SERVER可从cli获得,但DOCUMENT_ROOT密钥设置为空字符串。即->"。

你最好使用以下行中的东西来获得路径:

<?php echo dirname(__FILE__);?>
  //you can put this in variable, 
   $base_dir = dirname(__FILE__);
   //append another path ..
   $lib_dir = $base_dir . "/lib/";
   // Notice require does not need parentheses! ;)
   require_once $lib_dir . "db_connect.php";

有很多方法可以做到这一点,但这应该会给你一个想法。

编码快乐!

documentroot在从CLI 调用时为空

使用"__DIR__":http://php.net/manual/en/language.constants.predefined.php