0

I got this error when I am trying to run a simple PhP script via php.exe. The name of the script is gulliver ( no extension), and I type this into my command prompt:

php %Dir%\gulliver

Here's the content in the gulliver file:

<?php
//***************** Operating Systems parameters  **************************
  if ( PHP_OS == 'WINNT' ) 
    define('PATH_SEP', '\\');
  else
    define('PATH_SEP', '/');

//***************** Defining the Home Directory *********************************
  $docuroot =  explode ( PATH_SEP , $_SERVER['PWD'] );

The error occured was PHP notice: Undefined index: PWD in %Directory%gulliver.

Any idea how to solve this problem?

Graviton
  • 76,900
  • 138
  • 399
  • 575
  • 1
    If it helps anyone when running a script via sudo (such as sudo my_script.php) then I don't see $_SERVER['pwd'] when otherwise I do. – Alistair Nov 23 '12 at 00:22
  • @Alistair: Thanks for the hint. Reason behind it is, [that `PWD` (Posix Ohter Environment Variables)](http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html#tag_08_03) is an environment variable and not all are kept by `sudo` command by default. See [How to keep Environment Variables when Using SUDO](http://stackoverflow.com/q/8633461/367456). – hakre Aug 30 '15 at 08:35

2 Answers2

3

I could not find PWD in the manual page for $_SERVER. To find out which indexes are defined use var_dump($_SERVER);

If you need the "current working directory", use the getcwd() function.

PS: Instead of defining your own PATH_SEP, you could use the predefined constant DIRECTORY_SEPERATOR.

Bob Fanger
  • 24,735
  • 7
  • 52
  • 70
  • $_SERVER['PWD'] is not strictly an equivalent to getcwd() because getcwd resolves symlinks, which can cause problems in some situations (happened with drush module) – FGM Mar 29 '09 at 16:04
1

So, you execute the script through the CLI version of the interpreter and expect to see the $_SERVER array? Which is populated when a script is executed through a web server?

Milen A. Radev
  • 54,001
  • 19
  • 99
  • 105
  • 2
    The cli version also includes a (filled) $_SERVER array. It includes all environment variables amongst others. I agree the naming of the $_SERVER is weird. But that just shows PHP is designed for the web. – Bob Fanger Feb 19 '09 at 10:31