Sunday, April 3, 2011

Undefined index 'PWD' in $_server

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?

From stackoverflow
  • 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.

    FGM : $_SERVER['PWD'] is not strictly an equivalent to getcwd() because getcwd resolves symlinks, which can cause problems in some situations (happened with drush module)
  • 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?

    Bob Fanger : 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.

0 comments:

Post a Comment