running php script on windows via bat file returns error on require_once ($_SERVER['Document_Root']);
On Windows XP system, I have test.bat
C:\开发者_Python百科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?
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:
<?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";
There are many ways of doing this, but that should give you an idea.
Happy coding!
documentroot is empty when called from CLI
USE "__DIR__" : http://php.net/manual/en/language.constants.predefined.php
精彩评论